字符串的操作在开发过程中使用的比较多,以下是经常用到的一些方法
String demoString = "ceshibaixiaosheng";
//判断字符串是否包含 false
System.out.println(demoString.contains("t"));
//字符串相加 也可以使用+
String dsg = demoString.concat(" very good");
System.out.println(dsg); //ceshibaixiaosheng very good
//字符串长度 17
int length = demoString.length();
System.out.println(length);
//比较字符串是否相等 false
System.out.println("baixiaosheng".equals(demoString));
//截取字符串 角标从0开始
String str1 = demoString.substring(5,8);
System.out.println(str1); //bai
//从5开始到结尾
String str2 = demoString.substring(5);
System.out.println(str2);
//判断是否以某字符串开头
boolean sw = demoString.startsWith("ceshi");
System.out.println(sw);
//判断是否以某字符串结尾
boolean ew = demoString.endsWith("baixiaosheng");
System.out.println(ew);
//找出字符串在字符串中第一次出现的位置 如果没有返回-1
int indexOf = demoString.indexOf("bai");
System.out.println(indexOf); //5
//找出字符串中最后一次出现的位置
int lastIndex = demoString.lastIndexOf("e");
System.out.println(lastIndex); //14
//字符串字母都变成大写 CESHIBAIXIAOSHENG
System.out.println(demoString.toUpperCase());
//字符串字母都变成小写 ceshibaixiaosheng
System.out.println(demoString.toLowerCase());
//去掉首尾的空格
System.out.println(" baixiaosheng ".trim());
//替换字符串
String replace = demoString.replace("ceshi","AA");
System.out.println(replace); //AAbaixiaosheng
//将字符串中的第一个出现的字符串替换掉
String replaceF = demoString.replaceFirst("e","3");
System.out.println(replaceF); //c3shibaixiaosheng
//替换中所有的
String replaceAll = demoString.replaceAll("e","6");
System.out.println(replaceAll); //c6shibaixiaosh6ng