java字符串常用方法

柔光的暖阳◎ 2024-03-16 21:30 98阅读 0赞

文章目录

  • 将此字符串与指定对象进行比较。
  • 返回char指定索引处的值。
  • 测试字符串是否以指定后缀结尾
  • 测试字符串是否以指定前缀开头
  • 判读字符串是否为空
  • 求字符串长度
  • 指定子字符串的替代
  • 字符串拆分
  • 返回一个字符串,该字符串是此字符串的子字符串。
  • 将此字符串转化为新的字符数组
  • String所有字符转换为小写。
  • String所有字符转换为大写
  • 忽略大小写的比较
  • 去掉前导和尾随的空格

将此字符串与指定对象进行比较。

  1. String s1="abc";
  2. String s2="abc";
  3. System.out.println(s1==s2);//true
  4. String s3 = new String("abc");
  5. System.out.println(s1==s3);//false
  6. //boolean equals(Object anObject) 将此字符串与指定对象进行比较。
  7. boolean b1 = s1.equals(s2);
  8. System.out.println(b1);//true
  9. boolean b2 = s1.equals(s3);
  10. System.out.println(b2);//true

返回char指定索引处的值。

  1. //char charAt(int index)
  2. String s4 = "abcdef";
  3. char c1=s4.charAt(1);//b
  4. System.out.println(c1);

测试字符串是否以指定后缀结尾

  1. //boolean endsWitch(String suffix)
  2. String s5 = "2023年6月";
  3. boolean b3 = s5.endsWith("6月");
  4. System.out.println(b3);//true

测试字符串是否以指定前缀开头

  1. //boolean startsWitch(String suffix)
  2. String s6 = "2023年6月";
  3. boolean b4 = s6.startsWith("2023");
  4. System.out.println(b4);//true

判读字符串是否为空

  1. //boolean isEmpty()
  2. String s7 = "";
  3. boolean b5 = s7.isEmpty();
  4. System.out.println(b5); //true

求字符串长度

  1. //int length()
  2. String s8 ="abcdef";
  3. int len=s8.length();
  4. System.out.println(len);//6

指定子字符串的替代

  1. //replace(CharSequence target, CharSequence replacement)
  2. // 子字符串 替代字符串
  3. String massage="你真是TMD";
  4. String s9 = massage.replace("TMD", "***");
  5. System.out.println(s9);//你真是***

字符串拆分

  1. //split (String regex)
  2. String s10="李四,23";
  3. String[] sq1 = s10.split(",");
  4. System.out.println(Arrays.toString(sq1));//[李四,23]
  5. //以逗号来分割字符串,分割完后成为数组

返回一个字符串,该字符串是此字符串的子字符串。

  1. //substring (int beginIndex)
  2. // 索引
  3. String s11 ="abcdef";
  4. String sub1 = s11.substring(1);
  5. //返回从索引1开始的字符串
  6. System.out.println(sub1);//bcdef
  7. //substring(int beginIndex, int endIndex)
  8. // 起始索引 结束索引(取不到)
  9. String s11 ="abcdef";
  10. String sub2 = s11.substring(1, 3);
  11. //最大结束索引要不到
  12. System.out.println(sub2);//bc

将此字符串转化为新的字符数组

  1. //char[] toCharArray()
  2. String s11 ="abcdef";
  3. char[] c2 = s11.toCharArray();
  4. System.out.println(Arrays.toString(c2));//[a, b, c, d, e, f]

String所有字符转换为小写。

  1. //toLowerCase()
  2. String s12="ABCDEF";
  3. String sl = s12.toLowerCase();
  4. System.out.println(sl);//abcdef

String所有字符转换为大写

toUpperCase()

  1. //toUpperCase()
  2. String s13="abcdef";
  3. String st = s13.toUpperCase();
  4. System.out.println(st);//ABCDEF

忽略大小写的比较

  1. //equalsIgnoreCase(String anotherString)
  2. String s12="ABCDEF";
  3. String s13="abcdef";
  4. boolean b = s12.equalsIgnoreCase(s13);
  5. System.out.println(b);//true

去掉前导和尾随的空格

  1. //String trim()
  2. String s14=" abcdef ";
  3. System.out.println(s14);
  4. String tm = s14.trim();
  5. System.out.println(tm);
  6. //abcdef

学的不是技术,更是梦想!!!

发表评论

表情:
评论列表 (有 0 条评论,98人围观)

还没有评论,来说两句吧...

相关阅读