字符串的获取相关方法

字符串的获取相关方法

常用方法有:

public int lenght(); 获取字符串长度

public String concat(String str);拼接两个字符串

public char charAt(int index);返回指定索引的字符

public int indexOf(String str);查找子字符串出现在本字符串的首字符索引值,如果没有则返回-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class demo09{
public static void main(String[] args) {
String str = new String("abc");
String str1;
int len = 0;
int index = 0;
char ch;

len = str.length();//获取字符串长度
ch = str.charAt(0);//获取str的第0个索引的字符
str1 = str.concat("def");//拼接两个字符串
index = str.indexOf("ab");//查找子字符串第一次出现的索引值

System.out.println("字符串长度:" + len);
System.out.println("第0个字符是:" + ch);
System.out.println("拼接字符:" + str1);
System.out.println("字符串出现的" + index);

}
}