这里介绍四种:
String 当中与获取相关常用方法有:
public int length():获取字符串当中含有的字符个数,拿到字符串长度。
int | length() 返回此字符串的长度。 |
public string concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串
String | concat(String str) 将指定字符串连接到此字符串的结尾。 |
public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始)
char | charAt(int index) 返回指定索引处的 char 值。 |
public intdexOf(String str):查找参数字符串当中首次出现的索引位置,如果没有返回 -1值
int | indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。 |
public class Test04StringGet {
public static void main(String[] args) {
//获取字符串长度
int length = "jsdklfhjsdlifhlksdhoha".length();//按下ctrl+1可以快速生成返回值
System.out.println("获取的长度="+length);//获取的长度=22
//拼接字符串
String str1="Hello";
String str2="World";
String str3=str1.concat(str2);
System.out.println(str1);//Hello
System.out.println(str2);
System.out.println(str3);//HelloWorld 新的字符串
//获取指定索引的位置的单个字符
char ch = "Hello".charAt(1);
System.out.println("获取索引为1的位置"+ch);//result:e
//查找参数字符串在本来字符串当中出现第一次索引的位置
//如果根本没有,返回值-1
String original="HelloWorld";
int index = original.indexOf ("loW");
System.out.println("第一次出现索引的位置"+index);//3
System.out.println("HelloWorld".indexOf("wwww"));//-1
}
}