java字符串类_Java 字符串类

字符串类

String、StringBuffer、StringBuilder的区别?

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

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

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

String和StringBuffer的相互转换:

1、String —— StringBuffer

String s = "Java";

方式一:通过StringBuffer类的构造方法

StringBuffer sb = new StringBuffer(s);

方式二:通过StringBuffer类的append()方法

StringBuffer sb = new StringBuffer();

sb.append(s);

2、StringBuffer —— String

StringBuffer sb = new StringBuffer("Java");

方式一:通过String类的构造方法public String(StringBuffer sb)

String s = new String(sb);

方式二:通过StringBuffer的toString()方法

String s = sb.toString();

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)(4)将Stringbuffer字符串转换成String字符串

public String(StringBuffer buffer)

(5)将StringBuilder字符串转换成String字符串

public String(StringBuilder builder)

、判断功能

(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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值