String常用方法 1.
public class StringMethodTest {
@Test
public void test2() {
String s1 =“Hello world”;
String s2 =“hello world”;
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
字符串拼接
String s3 =“ZQH”;
String s4 =s3.concat(“Aim”);
System.out.println(s4);
比较两个字符串的大小
String s5 =“abc”;
String s6 = new String(“edf”);
System.out.println(s5.compareTo(s6));
}
—————————————————————————————————————
@Test
public void test1(){
String s1 = “Hello world”;
System.out.println(s1.length());
System.out.println(s1.charAt(0));
System.out.println(s1.charAt(8));
System.out.println(s1.isEmpty());
String s2 = s1.toLowerCase();
System.out.println(s1); // s1 不可变的,仍为原来的字符串
System.out.println(s2); // 改成小写以后的字符串
String s3 =" he llo world “;
String s4 = s3.trim(); // 去重空格
System.out.println(”-----"+s3+"-------");
System.out.println("-----"+ s4 +"-------");
}
}
String常用方法 2.
@Test
public void test3() {
测试字符串是否以指定的后缀结束
String str1 =“Helloworld”;
boolean b1 = str1.endsWith(“rld”);
System.out.println(b1); // true
测试字符串是否以指定的前缀开始
boolean b2 = str1.startsWith(“He”);// false
System.out.println(b2);
测试此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean b3 = str1.startsWith(“ll”,2);
System.out.println(b3);
String str2 = “world”;
System.out.println(str1.contains(str2));
System.out.println(str1.indexOf(“lol”));
System.out.println(str1.indexOf(“lol”,5));
String Str3 =“helloword”;
System.out.println(Str3.lastIndexOf(“ord”));
}
String常用方法 3.
@Test
public void test4() {
String str1 =“南京市姑苏区”;
/**
* 1.替换字符:replace
* 2.把出现南的字符改为北
*/
String str2 = str1.replace(‘南’,‘北’);
System.out.println(str1);
System.out.println(str2);
替换字符串
String str3 =str1.replace(“南京”, “上海”);
System.out.println(str3);
String str4 = “12345”;
boolean matches =str4.matches("\d+");
System.out.println(“machts”);
String tel =“0571-4532589”;
boolean result = tel.matches(“0571-\d{7,8}”); // matches
System.out.println(result); // true
}