练习String类的常用方法
实验要求
String str = "Hello World";
输出str长度
输出第一个"o",和最后一个"o"的索引
将str中的字符"l"替换成"m"
字符串str按空格“ ”分割为2个字符串,比较这两个字符串是否相等
public class TestString {
public static void main(String[] args) {
String str="HelloWorld";
System.out.println("字符串str的长度:"+str.length());
System.out.println("字符o在字符串str中第一次出现的位置:"+str.indexOf("o"));
System.out.println("字符o在字符串str中最后一次出现的位置:"+str.lastIndexOf("o"));
System.out.println("将l换成m:"+str.replaceAll("l", "m"));
String str1="H-e-l-l-o-W-o-r-l-d";
String[] sp=str.split("-");
sp=str1.split("-",2);
System.out.println("字符串str按“-”分为两份:");
for(int i=0;i<sp.length;i++){
System.out.println(sp[i]+"\t");
}
String s1="H";
String s2="elloWorld";
System.out.println("s1.equals(s2)的结果为:"+s1.equals(s2));
}
}