字符串的方法小结

来源:互联网 发布:平面转立体软件 编辑:程序博客网 时间:2024/05/22 09:04

字符串与多种数据类型均可发生转换,同时字符串之间还有多种运算,故字符串是一种非常重要的数据类型。

1)String 与 char[] 的相互转换

(1)char[] 转化为字符串

new String(char[] value)

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

valueOf(char[] data)

valueOf(char[] data,int offset,int count)

(2)字符串转化为字符数组

toCharArray()

2)String 与byte[] 的相互转化

(1)byte[] 转化为String

new String(byte[] bytes)

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

new String(byte[] bytes,Charset charset)       new String(byte[] bytes,String charsetName)

new String(byte[] bytes,int offset,int length,Charset charset)       new String(byte[] bytes,int offset,int length,String charsetName)

(2) String转化为byte[]

getBytes()

getBytes(Chaeset charset)

getBytes(String charsetName)

3)String与 StringBuffer的相互转化

(1)StringBuffer转化为String

new String(StringBuffer buffer)

toString()

(2)String转化为StringBuffer

new StringBuffer(String str)

String 与StringBuilder的转化同理

4)其他数据类型转化为String(char  double float long int boolean Object)

valueOf()

4)String之间的关系

(1)String 拆分成子字符串数组(使用正则表达式拆分)

split(String regex)

split(String regex,int limit)

(2)返回字符串的子字符串

subString(int beginIndex,int endIndex)

subString(int beginIndex)

(3)字符串的比较

CompareTo(String str)

CompareToIgnoreCase(String str)

(4)字符串的包含关系:

contains(charSequence s)

charSequence是一个接口,实现这个接口的类有:charBuffer  String StringBuffer StringBuilder这四个类。

(5)字符串的拼接

concat(String str)

将指定字符串拼接到此字符串的结尾

(6)字符串的大小写转化

toLowerCase()

toUpperCase()

(7)替换字符串中的某个字符

replace(char oldChar,char newChar)

(8)去空格

trim()

0 0