用于访问字符串对象的信息常用到的成员方法如下:
1.length():返回当前对象的长度
2.charAt(int index):返回当前串对象下标indexh处的字符。
3.indexOf(int ch):返回当前串中第一个与指定字符ch相同的下标,若找不到返回-1
"abcd".indexOf("c") //值为2
"abcd".indexOf("q") //值为-1
4.indexOf(String str,int fromIndex):从当前下标fromIndex初开始搜索返回第一个与指定字符串str相同的串的第一的字母在当期串中的下标,若找不到,则返回-1
"abcd".indexOf("cd",0) //值为2
5.subString (int beginIndex);返回当前串中从下标beginIndex开始到串尾的子串。
String S="abcde".subString(3); //s的值为“de”
6.String subString(int beginIndex, int endIndex):返回当前串中从下标beginIndex开始到下标endIndex-1的子串
* String s="abcdefj".subString(2,5) //值为:cde
public class Test7_2String {
public static void main(String[] args) {
//1
String s1="Java Application";
System.out.println("s1="+s1+"s1的长度 ="+s1.length());//s1=java Applications1的长度 =16
char cc[]= {'j','a','v','a',' ','A','p','p','l','e','t'};
int len=cc.length;//返回数组的长度
System.out.println(len);
//还可以这样 字符串.length
int length = "ABCD".length();System.out.println(length);//4
//2
char c1="12ABG".charAt(3);//返回字符串"12ABG的下标为3的字符
System.out.println("c1= "+c1);//B
char c2=s1.charAt(3); //返回S1字符串对象的下标为3的字符
System.out.println(c2);//a
//char c3=cc.cahrAt(1);不能这样用
//返回当前串内第一个与指定字符ch相同的字符的下标
int n1="abj".indexOf(97);
System.out.println(n1);//下标为0
int n2=s1.indexOf("J");
System.out.println(n2);//'J'所在的下标为0
int n3="abj".indexOf("bj",0);
System.out.println(n3);//从下标0开始搜索bj所在的位置为,1
int n4="abj".indexOf("bd",0);
System.out.println(n4);//从下标0开始搜索bd所在的位置为,没找到返回值为-1
int n5=s1.indexOf("va",1);
System.out.println(n5);//2
//返回当前串中的字串
String s2="abcdfg".substring(4);
String s3=s1.substring(4, 9);
System.out.println(s2);//返回当前串中的字串:fg
System.out.println(s3);//返回当前串中的字串: Appl
}
}