您的位置:首页 > 其它

String类的使用

2015-09-08 19:57 281 查看
实验任务

练习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));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: