String常用方法
- trim:去掉前后空格(不能去掉中间的空格)
- contains:检查是否规定的包含字符串
- endsWith:检查是否以规定的字符串结尾
- substring:截取两个下标之间的字符串
- split:用规定的字符串分割原有的字符串
- charAt:获取下标对应的字符
- length:获取字符串长度
- toCharArray:将字符串转变成数组
import java.util.Arrays;
public class Test6 {
public static void main(String[] args) {
String str = " a-b-b-s s-a-d ";
String trim = str.trim();
System.out.println("trim:去掉前后空格(不能去掉中间的空格)");
System.out.println(trim);
boolean a = str.contains("a");
System.out.println("contains:检查是否规定的包含字符串");
System.out.println(a);
boolean b = str.endsWith("a");
System.out.println("endsWith:检查是否以规定的字符串结尾");
System.out.println(b);
String s = str.substring(0, 5);
System.out.println("substring:截取两个下标之间的字符串");
System.out.println(s);
String[] s1 = str.split("-");
System.out.println("split:用规定的字符串分割原有的字符串");
System.out.println(Arrays.toString(s1));
char c = str.charAt(3);
System.out.println("charAt:获取下标对应的字符");
System.out.println(c);
int length = str.length();
System.out.println("length:获取字符串长度");
System.out.println(length);
char[] chars = str.toCharArray();
System.out.println("toCharArray:将字符串转变成数组");
System.out.println(Arrays.toString(chars));
}
}
运行结果