public char charAt(int index)
【根据索引取得指定位置上的字符】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.charAt(0));
}
}
输出H。【字符串的索引从0开始计算】
返回值为char类型
public boolean endsWith(String suffix)
【判断字符串是否以指定的内容结束,如果是返回true,否则返回false】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.endsWith("d"));
}
}
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.startsWith("H"));
}
}
该方法区分大小写。
返回值为boolean类型
public byte【】 getBytes()
【将一个字符串转换成一个byte类型的数组返回】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
byte [] bts = str.getBytes();
System.out.println("字节数组的长度:"+bts.length);
}
}
长度是11,空格也算
public int indexOf(String str)
【查询出指定内容在字符串中第一次出现的位置,如果没有指定的值返回-1】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.indexOf("l"));
}
}
三个l,只返回第一个l,返回2
public String intern()
【让字符串入池,转移到String常量池】
public class Hello {
public static void main(String[] args) {
String str1 ="Hello World";
String str2=new String("Hello World");
String str3= str2.intern();
System.out.println(str1==str2);
}
}
public boolean isEmpty()
【判断字符串的长度,如果长度为0返回true,否则返回false】
public class Hello {
public static void main(String[] args) {
String str1 ="";
System.out.println(str1.length());
System.out.println(str1.isEmpty());
}
}
public int lastIndexOf(String str)
【返回指定内容在字符串中最后一次出现的位置,如果没有出现返回-1】
public class Hello {
public static void main(String[] args) {
String str1 ="Hello World";
System.out.println(str1.lastIndexOf("o"));
}
}
输出为7。
public boolean matches(String regex)
【使用指定正则匹配字符串,如果匹配上则返回true,否则返回false】
public class Hello {
public static void main(String[] args) {
String str1 ="Hello World";
System.out.println(str1.matches("Hello World"));
}
}
参数还可以是正则表达式,正则的内容后面花两节课时间讲解。
public String【】 split(String regex)
【把字符串按照指定的字符串拆分,拆分之后保存到一个字符串数组返回】
以空格拆分
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
String [] strs =str.split(" ");
for(String temp:strs) {
System.out.println(temp+" ");
}
}
}
以o拆分
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
String [] strs =str.split("o");
for(String temp:strs) {
System.out.println(temp+" ");
}
}
}
Hell
W
rld
public String substring(int beginIndex)
【截取字符串,从指定的索引截取到最后,参数:beginIndex开始的索引下标】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.substring(2));
}
}
llo World
该方法切割的时候包括了开始的索引下标。
public String substring(int beginLndex,int endlndex)
【截取字符串,从索引截取到结束的索引】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.substring(1,3));
}
}
el
包括开始的索引不包括结束的索引
public String toLowerCase()
【将所有的字母换成小写】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.toLowerCase());
}
}
该方法不影响原字符串的内容。
public String toUpperCase()
【将所有的字母转换成大写】
public class Hello {
public static void main(String[] args) {
String str ="Hello World";
System.out.println(str.toUpperCase());
}
}
对原字符串没有影响。
public String trim()
【删除字符两端的空格,返回删除空格后的字符串】
public class Hello {
public static void main(String[] args) {
String str =" Hello World ";
System.out.println(str.length());
System.out.println(str.trim().length());
System.out.println(str.length());
}
}
总结:
1.掌握String类常用的方法
2.如果我们要分析一个方法(根据api文档或者别人写的方法),要从方法的返回值和方法的参数入手。