方法 描述
int indexOf(String s) 字符串中指定查找,返回位置
int lastIndexOf(String s) 最最后一次出现
char charAt(int) 获取指定索引位置的字符
String substring(int beginIndex) 切片操作(从指定位置到末尾)
String substring(int beginIndex, int endIndex) 切片操作(指定区间,左闭右开)
.trim() 去除空格
String replace(olderChar, newChar) 字符串替换
boolean stratsWith(String prefix) 是否存在以prefix为开头
boolean endsWith(String suffix) 是否存在suffix为后缀
boolean equals(String otherstr) 判断是否相同
boolean euqalsIngnoreCase(String otherstr) 判断是否相同(忽略大小写)
.toUpperCase() 转换全大写
.toLowerCase() 转换全小写
.split(String sign) 依据sign分割字符串

字符串查找

  • indexOf (String s)
  • lastIndexOf (String str)

    1. String str ="We are students";
    2. int size = str.indexOf("a"); // 变量size的值是3

    获取指定索引位置的字符

  • charAt()

    1. String str = "hello word";
    2. char mychar = str.charAt(5); // mychar的结果是w

    获取子字符串

  • substring(int beginIndex)

    1. String str = "Hello word";
    2. String substr = str.substring(3); //获取字符串,此时substr值为lo word
  • substring(int beginIndex,int endIndex)

    去除空格

  • trim()

    字符串替换

  • replace(oldChar,newChar)

    1. String str= "address";
    2. String newstr = str.replace("a", "A");// newstr的值为Address

    字符串开始与结尾

  • startsWith(String prefix) 返回boolean类型

  • endsWith(String suffix)

    判断字符串是否相等

  • equals(String otherstr)

  • equalsIgnoreCase(String otherstr)

    字母大小写转换

  • str.toLowerCase();

  • str.toUpperCase();

    字符串分割

  • split(String sign)