- equals:用于判断s1和s2是否相等和==的作用有一些差别
String s1 = “hello”;
String s2 = new String(“hello”);
boolean f = s1.equals(s2);
System.out.println(f);
答案:true -
length:判断长度和大小; String s="Helloworld,ems,jerry"; System.out.println(s.length());//方法 字符串的长度
-
System.out.println(s.toLowerCase()); //转换为小写得到新的字符串是更改好的内容,源字符串没变。String是不可变的 System.out.println(s.toUpperCase());//转换为大写
-
int index = s.indexOf('e');//检索字符或字符串出现的位置,如果找到就返回索引位置,如果找不到就返回-1 System.out.println(index);//检索字符串出现的位置
-
int index = s.indexOf('e',11);//从11索引开始找e出现的位置 System.out.println(index);//检索字符串出现的位置
-
index=s.lastIndexOf("e");//最后一次出现的索引位置
-
String s1="heLLo"; boolean f = "hello".equalsIgnoreCase(s1);//忽略大小写比较 System.out.println(f);
- s=“Helloworld,ems,jerry”
String news = s.substring(0,10);//截取从start索引位置开始,到end结束位置,包含前面的索引,不包含后面的索引
System.out.println(news+"-----"+s);
news = s.substring(11,14);//截取从start索引位置开始,到end结束位置,包含开始11的索引,不包含结束14的索引 - String news1 = news.concat(" very good");//字符串的拼接,相当于+
System.out.println(news1+"----"+news); - System.out.println(s.contains(“ems”));//是否包含某些字符串
- System.out.println(s.startsWith(“Hello”));//以某些字符串开头
- System.out.println(s.endsWith(“jerry”));//以某些字符串结尾s
- s=" hello world ";
news = s.trim();//去除前后空格 - s=“hello world ,hello java,hello linux”;
news = s.replace(“hello”,“你好”);//替换
System.out.println(news);
System.out.println(s); - // \s 空格 *正则规则 任意个数。
news = s.replaceAll("\s","**");//满足正则规则的替换
System.out.println(news); - s=“hello-world-java-html-jsp-servlet”;
String[] arr = s.split("-");//拆分字符串得到一个数组
System.out.println(Arrays.toString(arr)); - String newss = String.join(";",arr);//hello 将数组中的每个元素按照符号拼接成一个字符串
System.out.println(newss); - System.out.println(“abc”.compareTo(“aba”));//比较相同字母的个数
Java中String常用类
最新推荐文章于 2024-08-19 16:01:20 发布