string 方法使用总结

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 );

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值