天天看点

String类

常见构造方法

• public string():空构造

• public string(byte[] bytes):把字节数组转成字符串

• public string(byte[] bytes,int index,int length):把字节数组的一部分转成字符串

• public string(char[] value):把字符数组转成字符串

• public string(char[] value,int index,int count):把字符数组的一部分转成字符串

• public string(string original):把字符串常量值转成字符串

string类的判断功能

• boolean equals(object obj):比较字符串的内容是否相同,区分大小写

• boolean equalsignorecase(string str):比较字符串的内容是否相同,忽略大小写

• boolean contains(string str):判断大字符串中是否包含小字符串

• boolean startswith(string str):判断字符串是否以某个指定的字符串开头

• boolean endswith(string str):判断字符串是否以某个指定的字符串结尾

• boolean isempty():判断字符串是否为空。

string类的获取功能 //不存在则返回-1

• int length():获取字符串的长度。

• char charat(int index):获取指定索引位置的字符

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

• int indexof(string str):返回指定字符串在此字符串中第一次出现处的索引。

• int indexof(int ch,int fromindex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。

• int indexof(string str,int fromindex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

• lastindexof

//包含头,不包含尾,左闭右开,substring会产生一个新额字符串,需要将新的字符串记录

• string substring(int start):从指定位置开始截取字符串,默认到末尾。

• string substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

string类的切割功能:public string[] split(string regex)

string类的替换功能:public string replaceall(string regex,string replacement)

string类的转换功能

• byte[] getbytes():把字符串转换为字节数组。

gbk码表一个中文代表两个字节,gbk码表特点,中文的第一个字节肯定是负数

• char[] tochararray():把字符串转换为字符数组。

• static string valueof(char[] chs):把字符数组转成字符串。底层是由string类的构造方法完成的

• static string valueof(int i):把int类型的数据转成字符串。

• 注意:string类的valueof方法可以把任意类型的数据转成字符串。

• string tolowercase():把字符串转成小写。(了解)

• string touppercase():把字符串转成大写。

• string concat(string str):把字符串拼接。

用+拼接字符串更强大,可以用字符串与任意类型相加,concat方法调用的和传入的都必须是字符串

string的替换功能 //不存在,保留原字符不改变

• string replace(char old,char new) //替换了里面所有old字符

• string replace(string old,string new)

string的去除字符串两空格功能

• string trim() //去除两端的空格

string的按字典顺序比较两个字符串

• int compareto(string str)

• int comparetoignorecase(string str)(了解)