Java字符串

String、StringBuffer、StringBuilder的区别?

       1、String内容不可变,StringBuffer、StringBuilder内容可变

       2、StringBuffer:同步,线程安全,效率低;StringBuilder:不同步,线程不安全,效率高

       注意:StringBuilder类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。

String类

    String类的常用构造方法:           

  (1)无参构造
       public String()

(2)将字节数组转换成字符串
       public String(byte[] bytes) 

public String(byte[] bytes,int offset,int length)

(3)将字符数组转换成字符串

public String(char[] value)

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

<一>、判断功能  
       (1)public boolean equals(Object anObject):比较字符串的内容是否相同,区分大小写。
       (2)public boolean equalsIgnoreCase(String anotherString):比较字符串的内容是否相同,忽略大小写。

(3)public boolean contains(String str):判断字符串中是否包含制定字符串。

(4)public boolean startsWith(String prefix):判断字符串是否以指定的字符串开头。

(5)
public boolean endsWith(String suffix):判断字符串是否以指定的字符串结尾。

(6)public boolean isEmpty():判断字符串是否为空。

注意:字符串内容为空和字符串对象为空不同,如:String s = ""; String s = null;    
   <二>、获取功能 
       (1)public int length():获取字符串的长度

(2)public char charAt(int index):获取指定索引位置的字符

(3)public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引

(4)public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
       (5)public int indexOf(String str):返回指定字串在此字符串中第一次出现处的索引
       (6)public int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引

(7)public int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引
       (8)public int lastIndexOf(int ch, int fromIndex):返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索

(9)public int lastIndexOf(String str):返回指定字串在此字符串中最右边出现处的索引
       (10)public int lastIndexOf(String str, int fromIndex)::返回指定字符串在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索
       (11)public String substring(int beginIndex):从指定位置开始截取字符串,默认到结尾

(12)public String substring(int beginIndex,int endIndex):从指定位置开始到指定位置结束截取字符串

<三>、转换功能

(1)public byte[] getBytes():把字符串转换成字节数组

(2)public char[] toCharArray():把字符串转换成字符数组

(3)public static String valueOf(char[] chs):把字符数组转换成字符串

(4)public static String valueOf(char[] chs, int offset,int count):把字符数组的一部分转换成字符串

(5)public static String valueOf(int i):把int类型的数据转换成字符串

注意:String类的valueOf方法可以把任意类型的数据转换成字符串,包括:boolean,char,int,float,double,long,Object等。

(6)public String toLowerCase():把字符串转换成小写

(7)public String toUpperCase():把字符串转换成大写

(8)public String concat(String str):把指定字符串拼接到此字符串的结尾

<四>、替换功能

(1)public String replace(char oldChar, char newChar):把字符串中的oldChar替换为newChar

(2)public String replace(String oldStr, String newString):把字符串中的oldStr替换为newStr

<五>、拆分功能
         public String[] split(String regex):根据给定正则表达式的匹配拆分字符串

<六>、去掉开头和结尾的空格
         public String trim():去掉字符串开头和结尾的空格

<七>、比较功能

(1)public int compareTo(String anotherString):按字典顺序比较两个字符串,区分大小写

(2)public int compareToIgnoreCase(String str):按字典顺序比较两个字符串,忽略大小写

StringBuffer类

      Stringbuffer类的常用构造方法:

     (1)无参构造,初始容量为16个字符

              public  StringBuffer()

    (2)指定容量的字符串缓冲区对象
        public StringBuffer(int capacity)

(3)指定字符串内容的字符串缓冲区对象,初始容量为16+字符串参数的长度
        public StringBuffer(String str)  

<一>、添加功能
        (1)public StringBuffer append(String str):把字符串添加到字符串缓冲区,返回的是字符串缓冲区本身
           注意:StringBuffer类的append方法可以把任意类型的数据添加到字符串缓冲区,包括:boolean,char,char[],int,float,double,long,Object,StringBuffer等

(2)public StringBuffer insert(int offset,String str):在指定位置把字符串插入到字符串缓冲区,返回的是字符串缓冲区本身
          注意:StringBuffer类的insert方法可以把任意类型的数据插入到字符串缓冲区,包括:boolean,char,char[],int,float,double,long,Object,StringBuffer等

<二>、度量功能

(1)public int capacity():返回当前容量

(2)public int length():返回长度(字符数)

(3)public void trimToSize():Attempts to reduce storage used for the character sequence.
     <三>、删除功能

(1)public StringBuffer deleteCharAt(int index):删除制定位置的字符,并返回本身

(2)public StringBuffer delete(int start, int end):删除从指定位置开始到指定位置结束的内容,并返回本身

参数:
start - The beginning index, inclusive.
end - The ending index, exclusive.

<四>、替换功能
         (1)public void setCharAt(int index,char ch):The character at the specified index is set to ch.
         (2)public StringBuffer replace(int start, int end, String str):从start开始到end结束的内容替换为str

参数:
start - The beginning index, inclusive.
end - The ending index, exclusive.
str - String that will replace previous contents.

<五>、反转功能

public StringBuffer reverse():将字符串反转,并返回本身

<六>、截取功能
          (1)public String substring(int start):从指定位置开始截取字符串,默认到结尾,返回的是String类型数据
          (2)public String substring(int start, int end):从指定位置开始到指定位置结束截取字符串,返回的是String类型数据

<七>、获取功能
(1)public char charAt(int index):Returns the char value in this sequence at the specified index.

(2)public int indexOf(String str):Returns the index within this string of the first occurrence of the specified substring.
          (3)public int indexOf(String str, int fromIndex):Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
          (4)public int lastIndexOf(String str):Returns the index within this string of the rightmost occurrence of the specified substring.
          (5)public int lastIndexOf(String str, int fromIndex):Returns the index within this string of the last occurrence of the specified substring.
       <八>、转换功能
            public String toString():将StringBuffer类型转换成String类型

StringBuilder类

        一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。

       StringBuilder类的相关方法和StringBuffer大致相同,具体参见StringBuffer。



 


原文地址:https://www.cnblogs.com/zfsky/p/5940800.html