5.3获取字符串信息(长度length,首次出现indexof,未次出现lastindexof,取索引字符)

5.2.1连接多个字符串

【1】可以使用“+”运算符可以连接多个运算符并产生一个String对象

String A="I ";
String B="LOVE";
String C=" you";
System.out.print(A+B+C);

//结果为 I LOVE you

 

5.2.2 连接其他数据类型

String A="I ";
String B="LOVE";
String C=" you ";
int D=6;
double E=2.5;
System.out.print(A+B+C+(D+E)+" year");

//I LOVE you 8.5 year

5.3.1 获取字符串长度   str.length()

String A="I ";
String B="LOVE";
String C=" you ";
int D=6;
double E=2.5;
String F=A+B+C+(D+E)+" year";
System.out.print(F.length());

//19

5.3.2字符串查找  str.indexof(String s)/   str.lastindexof(String s)

String A="I ";
String B="LOVE";
String C=" you ";
int D=6;
double E=2.5;
String F=A+B+C+(D+E)+" year";
//F=I LOVE you 8.5 year
int index =F.indexOf("you");
System.out.print(index);
//7
index =F.indexOf("k");
System.out.print(index);
//-1

indexof:返回的是搜索的字符或字符串首次出现的位置

lastindexof:返回的是搜索的字符或字符串最后一次出现的位置

        若lastindexof(“”)则返回str.length()一样的结果

int index ="AAAAAAAAA".lastIndexOf("A");
System.out.print(index);
//8
index ="AAAAAAAAA".lastIndexOf("");
System.out.print(index);
//9

5.3.3 获取指定索引位置的字符  str.charAt(int index)

String A="I ";
String B="LOVE";
String C=" you ";
int D=6;
double E=2.5;
String F=A+B+C+(D+E)+" year";
//F=I LOVE you 8.5 year
char result=F.charAt(2);
System.out.print(result);

//L

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值