Java String类的使用

1、字符串查找

(1)indexOf  返回字符/字符串第一次出现的位置下标,没有就返回-1

类似于c++ string.find和QT QString.indexOf

String s1 = "hello";
int index = s1.indexOf('l');
System.out.println(index);//2
int index2 = s1.indexOf('l',3);
System.out.println(index2);//3

(2)lastIndexOf  从后往前找字符/字符串,返回从后往前第一次出现ch字符的下标,没有找到返回-1

类似于c++ string.rfind和QT QString.lastIndexOf

String s2 = "helloxawllxhxawllxh";
int index3 = s2.lastIndexOf('a');
System.out.println(index3);//13

2、截取子字符串

(1)substring  从字符串中截取后面字符串的内容

类似于c++ string.substr和QT QString.mid

String str = "adsasdasdasdasd";
String ret = str.substring(4);
System.out.println(ret);//sdasdasdasd

3、字符串和数字相互转换

(1)String.valueOf()  数字转字符串

String str = String.valueOf(1234);
String str1 = String.valueOf(12.34);
System.out.println(str);
System.out.println(str1);

(2)Integer.valueOf() 或 Integer.parseInt  字符串转数字

int a = Integer.valueOf("1234");
System.out.println(a);//1234
String minute_s = index_s.substring(1, 3);
String second_s = index_s.substring(4, 6);
int timeIndex = Integer.parseInt(minute_s)*60 + Integer.parseInt(second_s);

4、字符串格式化

(1)String.format

String text = String.format("%02d:%02d/%02d:%02d", min, sec, totalMin, totalSec);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值