美文网首页
java  String类常用操作字符串方法

java  String类常用操作字符串方法

作者: 何佳阳 | 来源:发表于2020-04-10 09:20 被阅读0次

    String类包括很多方法,以方便操作字符串,比如:

    public boolean isEmpty() //判断字符串是否为空

    public int length() //获取字符串长度

    public String substring(int beginIndex) //取子字符串

    public String substring(int beginIndex, int endIndex) //取子字符串

    public int indexOf(int ch) //查找字符,返回第一个找到的索引位置,没找到返回-1

    public int indexOf(String str) //查找子串,返回第一个找到的索引位置,没找到返回-1

    public int lastIndexOf(int ch) //从后面查找字符

    public int lastIndexOf(String str) //从后面查找子字符串

    public boolean contains(CharSequence s) //判断字符串中是否包含指定的字符序列

    public boolean startsWith(String prefix) //判断字符串是否以给定子字符串开头

    public boolean endsWith(String suffix) //判断字符串是否以给定子字符串结尾

    public boolean equals(Object anObject) //与其他字符串比较,看内容是否相同

    public boolean equalsIgnoreCase(String anotherString) //忽略大小写比较是否相同

    public int compareTo(String anotherString) //比较字符串大小

    public int compareToIgnoreCase(String str) //忽略大小写比较

    public String toUpperCase() //所有字符转换为大写字符,返回新字符串,原字符串不变

    public String toLowerCase() //所有字符转换为小写字符,返回新字符串,原字符串不变

    public String concat(String str) //字符串连接,返回当前字符串和参数字符串合并结果

    public String replace(char oldChar, char newChar) //字符串替换,替换单个字符        //字符串替换,替换字符序列,返回新字符串,原字符串不变

    public String replace(CharSequence target, CharSequence replacement)

    public String trim() //删掉开头和结尾的空格,返回新字符串,原字符串不变

    public String[] split(String regex) //分隔字符串,返回分隔后的子字符串数组

    相关文章

      网友评论

          本文标题:java  String类常用操作字符串方法

          本文链接:https://www.haomeiwen.com/subject/upjlmhtx.html