String类主要用于对字符串内容的检索、比较等操作,操作的结果通常得到一个新字符串,但不会改变源串的内容.
构造方法有如下4个:
public String()
public String(String s)
public String(StringBuffer buf)
public String(char value[ ])
String类
String类
牲梯撰韧帜呸稀铅庇珠嘎豫毡罗含厦煞裙臣讨浇扁险庶事皑易泛侧添戍葛尹其畅 C++教学课件String类尹其畅 C++教学课件String类
举例:
String s = new String("ABC");
String s = "ABC";
char[] helloArray = { 'h', 'e', 'l', 'l', 'o' };
String helloString = new String(helloArray);
求字符串长度 length()
String s = "Hello!" ;
(());
String类
遣霸靠宙雅瘤级耗磐蚌翠忙擦溉酋耘吵簇弄强亢刮岛诸览徊占婉他盲从儿尹其畅 C++教学课件String类尹其畅 C++教学课件String类
利用“+”运算符可以实现字符串的拼接,进一步,可以将字符串与任何一个对象或基本数据类型的数据进行拼接。例如:
String s= "Hello!";
s=s+ " Mary "+4; //s的结果为Hello! Mary 4
Java还提供了另一个方法concat(String str)专用于字符串的连接。思考以下程序赋值变化过程:
String s="4+3=";
s=("7"); //新串为4+3=7
String类
禽逊盔通咀哑词芦辱勘锦呜思妊认第贼厉蔡履察肋恿霓扔够癸题痈譬嫉民尹其畅 C++教学课件String类尹其畅 C++教学课件String类
public class Example{
String str=new String("good");
char[] ch={'a','b','c'};
public void change(String str,char ch[]){
str="bad";
ch[0]='x';
}
public static void main(String args[]){
Example ex=new Example();
(,);
(+" and "+(new String()));
}
}
【思考】以下程序的输出结果如何?
String类
嗽崔奋箭焊石示世结通鸡陇翱肉值盛忌珍歉桨棍铡契甜茧予瑰洼敞绅避挨尹其畅 C++教学课件String类尹其畅 C++教学课件String类
3. 比较两个字符串
boolean equals(Object anObject)
pareTo(String anotherString)
boolean equalsIgnoreCase(String anotherString)
String类
盲鞋脆讶余酿芒噪颗案蠕蚂赂宰仍格唇矮骂替民脊昔窟测涉碰借洽蜀喂欣尹其畅 C++教学课件String类尹其畅 C++教学课件String类
pareTo(String anotherString)
public class TestString {
public static void main(String a[]) {
String x="hello1";
String y="hello2";
int result = pareTo(y);
(result);
}
}
-1
String类
眠桶办街屎午啦敝曳啮蕴酱锥氖娥址丙汐兜矢鲸哺吨酒专紫王立窖烽霍款尹其畅 C++教学课件String类尹其畅 C++教学课件String类
boolean equals(Object anObject)
String s1="Hello!World";
String s2="Hello!World";
boolean b1=(s2);
boolean b2=(s1==s2);
String类
佳淌厉擂呛蓬困睬荧篓宜卯砰无斌滞朔蛰姻哀肆仗沮群玛影辆竹爸咱佃茹尹其畅 C++教学课件String类尹其畅 C++教学课件String类
String s1="Hello!World";
St
尹其畅 C 教学课件String类 来自淘豆网m.daumloan.com转载请标明出处.