java中的字符串一

public class TestString2 {

public static void main(String[] args) {
//判断两字符串是否相等
String s1 = "Hello world";
String s2 = "Hello java";
if (s1.equals(s2)) {
System.out.println("equal");
}else{

System.out.println("not equal");
}
System.out.println("------------");
//判断s1是否以He开头
if (s1.startsWith("He")) {
System.out.println("begin with He");
}
//判断s1是否以He结尾
if (s1.endsWith("world")) {
System.out.println("end with world");
}
//返回s1中字母d的下标
System.out.println("返回s1中字母d的下标为:"+s1.indexOf('d'));
//返回索引从3到8的子字符串
System.out.println("返回索引从3到8的子字符串" + s1.substring(3,8));
//拼接
System.out.println("拼接到后面"+s1.concat("abcd"));
//替换单个字母
System.out.println("替换单个字母"+s1.replace('H','z'));
//替换第一个字母
System.out.println("替换第一个字母"+s1.replaceFirst("H","AABB"));
//是否包含world
System.out.println("是否包含world "+ s1.contains("world"));
//根据空格切割
System.out.println("根据空格切割 "+s1.split(" ")[0] +" " + s1.split(" ")[1] );
}

}

原文地址:https://www.cnblogs.com/51testing/p/7872789.html