Java常用类 --java.lang.String

1、String类常用的构造函数

    public String(String original)
   使用串对象original,创建字符串对象,其中original可以是字符串常量或字符串对象。

   public String(char value[])

   使用字符数组value,创建一个字符串对象。

   public String(char value[],int offset,int count)

   从字符数组value下标为offset的字符开始,创建还有count个字符的字符串对象。

   public String(StringBuffer buffer)

   使用StringBuffer类的对象buffer,创建一个字符串对象。


2、String类常用的方法一

    public char charAt(int index)
    返回字符串中index位置处的字符,index从0开始

    public int compareTo(String anotherString)
    比较当前字符串与anotherString字符串的大小。若当前字符串大,则返回正整数;当前字符串小,则返回一个小于0的整数;若两者相等,则返回0

    public int compareToIgnoreCase(String anotherString)
    比较两个字符串的大小,比较时,忽略大小写;返回结果和compareTo方法一致

    public String concat(String str)
    在当前字符串尾部追加字符串str,并返回连接后的新字符串

    public boolean endsWith(String suffix)
    若当前字符串以字符串suffix结尾,则返回true,否则返回false

    public boolean equals(Object anObject)
    若当前字符串对象与anObject拥有相同的字符串时,返回true,否则返回false

    public boolean equalsIgnoreCase(String anotherString)

    功能同equals(),但比较两字符串时,忽略大小写


3、String类常用的方法二

    int indexOf(int ch)
    返回指定字符ch在此字符串中第一次出现的位置下标(下标从0开始)。若找不到,则返回-1

    int indexOf(int ch,int fromIndex)
    从当前字符串中下标为fromIndex处开始查找字符ch,并返回ch在字符串中首次出现的位置坐
标。

    int indexOf(String str)
    返回字符串str在当前字符串中首次出现的位置下标

    int indexOf(String str,int fromIndex)
    从当前字符串中下标为fromIndex处开始查找字符串str,并返回字符串str在当前字符串中首
次出现的位置下标
    各自对应的lastIndexOf(...)方法:表示从当前字符串的尾部开始查找;

    boolean contains(CharSequence s) 

    当且仅当此字符串包含指定的 char 值序列s时,返回 true。


4、String类常用方法三

    char[] toCharArray()
    将此字符串转换为一个新的字符数组。

    int length()
    返回此字符串的长度。

    boolean isEmpty()
    当且仅当 length() 为 0 时返回 true。

    String replace(char oldChar, char newChar)
    返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

    boolean startsWith(String prefix)
    测试此字符串是否以指定的前缀开始。

    String substring(int beginIndex)
    返回一个新的字符串,它是此字符串的一个子字符串。

    String substring(int beginIndex, int endIndex)
    返回一个新字符串,它是此字符串的一个子字符串。

    String toLowerCase()
    将当前字符串转换为小写形式,并将其返回

    String toUpperCase()
    将当前字符串转换为大写形式,并将其返回

    String trim()
    去掉字符串的前后空格,并将其返回

猜你喜欢

转载自blog.csdn.net/weixin_41704428/article/details/80341155