变量和类型 | 方法 | 描述 |
---|---|---|
char | charAt(int index) | 返回指定索引处的char值 |
String | concat(String str) | 将指定字符串连接到此字符串末尾 |
int | indexOf(int ch) | 返回指定字符第一次出现的字符串的索引 |
int | indexOf(String str) | 返回指定子字符串第一次出现的字符串的索引 |
String | trim() | 返回一个删除前后空格的该字符串 |
String | substring(int begin) | 返回索引为begin开始的该字符串的子串 |
String | substring(int begin,int end) | 返回索引为begin开始,end-1结束的该字符串的子串 |
static String | valueOf(A a) | 返回A参数类型的字符串表现形式 |
charAt示例:
String str = "abcde";
char x = str.charAt(2);//x = 'c';
concat示例:
String str1 = "Hello,";
String str2 = "World!";
String str = str1.concat(str2);//str = "Hello,World!";
IndexOf示例:
String str = "aabbccdd";
int x = str.indexOf('b');//x = 2;
int y = str.indexOf("ccdd");//y = 4;
trim示例:
String str = " ab cde ";
System.out.println(str.trim());//输出结果为ab cde
substring示例:
String str = "abcdefg";
String str1 = str.substring(3);//str1 = "defg";
String str2 = str.substring(1,3);//str2 = "bc",返回索引为begin开始,end-1结束的该字符串的子串
valueOf示例:
int num1 = 123456;
String str = String.valueOf(num1);//str = "123456";
double num2 = 105.25;
String str1 = String.valueOf(num1);//str = "105.25";