string 方法使用总结

来源:互联网 发布:执行python脚本传参数 编辑:程序博客网 时间:2024/05/22 10:34

1.获取

(1)获取字符串的长度

int length(int index)

(2)根据字符串位置获取字符串上某个字符

char charAt(int index)

(3)根据字符获取该字符在字符串中的位置

int indexof(int ch)返回的是ch在字符串第一次出现的位置。

int indexof (int ch ,int fromIndex) 从fromindex指定位置开始,获取ch在字符串中出现的位置

int indexof(string str)返回的是字符串str在字符串中第一次出现的位置

int indexof(string str,int fromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置。

int lastindexof(int ch)反向索引

2.判断

(1)字符串是否包含某一个子串

boolean.contains(str) 判断字符串是否包含

(2)字符串是否有内容

boolean isEmoty() 原理就是判断字符串长度是否为0

(3)字符串是否以指定内容开头

boolean startswith(str)

(4)字符串是否以指定内容结尾

boolean endswith(str)

3.转换

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

构造函数:

             string(char[ ])

             string (char[] ,offset,count)将字符数组中的一部分转成字符串

(2)将字符串转换为字符数组

char[] tocharArray();

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

string (byte[])

string (byte[],offset,count)将字符数组中的一部分转成字符串

(4)将字符串转换成字节数组

byte[] getBytes()

(5)将基本数据类型转成字符串

static string valueof (int)

static string calueof(double)

4.替换

string replace(oldchar ,newchar)

5.切割

string[] split(rege x)

6.子串获取字符串中的一部分

string substring(begin)

string substring(begin ,end)

7.转换,取出空格,比较

(1)将字符串转换成大写或小写

string touppercase();

string tolowercase();

(2)将字符串两端的多个空格去掉

string trim();

(3)对两个字符串进行自然顺序的比较

int  compaveTo(string );

原创粉丝点击