指定字符的索引位置:s.indexOf()、s.lastIndexOf()  (以字符串s为例。第一次与最后一次出现的位置)

指定索引位置的字符:s.charAt()

除前、后空格:s.trim()

去除所有空格:s.replaceAll(" ","")   替换思路

替换第一个匹配的字符串:s.replaceFirst()

替换所有匹配的字符串:s.replace()

    String s="students";
    //索引位置
    System.out.println("第一次出现s的位置:"+s.indexOf("s")+"\n最后一次出现s的位置:"+s.lastIndexOf("s"));
    System.out.println("在索引位置1处的字符是:"+s.charAt(1));
    String s1="   a b   ";
    //去空格
    System.out.println("去除首尾空格后:"+s1.trim());
    System.out.println("去除所有空格后:"+s1.replaceAll(" ",""));
    String s2="bad bad boy";
    //替換字符串
    System.out.println(s2.replaceFirst("bad", "good"));
    System.out.println(s2.replace("bad", "good"));

运行结果: 字符串操作:索引位置、去空格、替换字符串

 

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
猜你喜欢
  • 2021-09-06
  • 2021-11-28
  • 2021-05-29
  • 2021-11-22
  • 2022-12-23
  • 2021-10-18
相关资源
相似解决方案