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
发布了8 篇原创文章 · 获赞 7 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/Kkjkjl/article/details/104135694