String常用方法
class TestArrayMethod{
public static void main(String[] args) {
String s1 = "Blue brigd";
//字符串替换
String s = "我是小学生";
s= s.replaceAll("我", "你");
System.out.println(s);
//判断某个字符串是否以某个字符开头
boolean a = s.startsWith("你");
System.out.println(a);
System.out.println("*************");
boolean b= s.startsWith("是", 3);
System.out.println(b);
//判断某个字符串是否以某个字符结尾
System.out.println( s.endsWith("我"));
//将字符串所有字母都变成大写
s1= s1.toUpperCase();
System.out.println(s1);
//从一个字符开始 到结束 返回一个新的字符串
String s5 = s1.substring(2);
System.out.println(s5);
//返回一个指定第一字符串到 指定最后一个字符串 (不包含最后一个字符串)
String s6 = s1.substring(1, 5);
System.out.println(s6);
System.out.println("*******************");
//teim去掉两边的空格 统计字数(不去掉中间的空格)
String s7 = " he llo ";
String s2 = s1.trim();
System.out.println(s2.length());
// 指定第一个字符到最后一个字符
String f = "sCHOASLQMVJ";
String g = f.substring(f.lastIndexOf("C") + 1);
System.out.println(g);//HOASLQMVJ
}
}
字符串替换:
**replaAll 将指定的所有 字符串 都进行替换 **
replaceFirst 仅替换 查找到的指定元素 第一个
//字符串替换
String s = "我是小学生";
s= s.replaceAll("我", "你");
System.out.println(s);
是否以某个字符开头
StartsWith()
boolean a = s.startsWith("你");
System.out.println(a);
System.out.println("*************");
boolean b= s.startsWith("是", 3);
System.out.println(b);
是否以某个字符结尾
endsWith()
System.out.println( s.endsWith("我"));
所有字母都变成大写
toUpperCase()
s1= s1.toUpperCase();
System.out.println(s1)
返回新的字符串
返回一个指定第一字符串到 指定最后一个字符串 (不包含最后一个字符串)
String s5 = s1.substring(2);
System.out.println(s5);
//返回一个指定第一字符串到 指定最后一个字符串 (不包含最后一个字符串)
String s6 = s1.substring(1, 5);
System.out.println(s6);
统计字数
(不去掉中间的空格) teim去掉两边的空格
String s7 = " he llo ";
String s2 = s1.trim();
System.out.println(s2.length());
指定两个字符
返回新的字符串
String f = "sCHOASLQMVJ";
String g = f.substring(f.lastIndexOf("C") + 1);
System.out.println(g);//HOASLQMVJ
字符串分割
**public String [] split(String regex) **
采用正则表达式切割字符串
**字符串分割 需要定义分割字符串的符号 **
( \ 如 括号 分割线等 需要用 "\"转译
//字符串分割
String a = "ada--csc--cssca";
String b[] = a.split("--");//定义一个数组 来接收分割的字符串
for(String c : b){//增强for循环 遍历数组
System.out.println(c);
}
综合练习
class TestArrayMethod {
public static void main(String[] args) {
String fileName = "20200210刘海龙Resume.docx";
//判断字符串是否以"2014""开头
System.out.println( fileName.startsWith("2014"));
//是否以docx 结尾
System.out.println(fileName.endsWith("docx"));
//字符是否以"doc" 结尾
System.out.println(fileName.endsWith("doc"));
//将字符都变为大写
System.out.println(fileName.toUpperCase());
//将字符都变成小写
System.out.println(fileName.toLowerCase());
//截取第九个位值到结尾的字符串
String g = fileName.substring(9);
System.out.println(g);
//截取 第 九个到第11个位置的字符串
System.out.println(fileName.substring(8, 10));
//截取 刘后面的字符串
System.out.println(fileName.substring(fileName.indexOf("刘")));
//忽略字符串前后空格
String fileName2 = " 20200201刘海龙Resume .dox";
System.out.println(fileName2.trim());
System.out.println(fileName2.length());
}
}