1字符串有string stringBuffer
2定义字符串 String str;
str=“abc”;
System.out.print(str);
(1) 创建两个字符串s1,s2,内容分别是“17软件3班”和自己的姓名比如“王淼”;创建一个新的字符串s3,将s1和s2连接一起赋给s3;求出s3的长度并输出
String S1,S2,S3;
S1=“17软件3班”;
S2=“王淼”;
S3=S1+S2;System.out.print( S3.length());
(2)创建字符串s6=” How are you Teacher”;
1)检验字符串“are”和字符’a’在s6中首次出现的位置和最后出现的位置,并输出。
求位置str.indexOf()
lastIndexOf(最后出现)
2)去掉s6两端的空格,并赋给s6并输出。
去掉空格 str.trim()
3)将s6都转换成小写字母,并输出。
转换小写字母
toLowerCase
4)从s6中截取“teacher”,赋值给s7并输出。
截取
substring
5)把s6中的字符e都替换成b,赋值给s8并输出。
替换
replaceString
程序:
s6=" How are you Teacher ";
System.out.println("are首先出现的位置;"+s6.indexOf("are"));
System.out.println("a首先出现的地方;"+s6.indexOf('a'));
System.out.println("a最后出现的地方;"+s6.lastIndexOf('a'));
System.out.println("去掉开头结尾的空格后的字符串;"+s6.trim());
String a=s6.toLowerCase();
System.out.println("大写转换成为小写;"+a);
String s7=s6.substring(s6.indexOf("Teacher"));
String s8=s6.replace("e","b"); System.out.println("截取字符串;"+s7+"\n"+"e替换成b;"+s8);