/*
* java.lang.String类中常见的方法:(字符串处理常用的方法)
* 1.参数不严格 ; 2,字符串支持正则表达式
*
* 1、length():
* 制定一个字符串的长度;
*
* 2、substring(int begin)
* substring(int beginindex, int endindex)
* substring(),参数从零开始数字符串;
*
* 3、split(String s1)
* split(String s2, int i)
*
* 4、startsWith(String s1)
* startsWith(String s2, int i)
* endsWith(String s3)
*
* 5、replace(String sb, String st)
* replaceAll(String sb, String st)
*
*/
public class TestString {
public static void main(String[] args){
new TestString().testLength();
new TestString().testSubstring();
new TestString().testSplit();
new TestString().testStartsendsWith();
new TestString().testReplaceAll();
}
// 1、length():
public void testLength(){
String slen = "abc";
String slenn = new String("abcdefg");
int i = slen.length();
int k = slenn.length();
System.out.println(i);
System.out.println(k);
System.out.println("-----------------------------");
}
// 2、substring(int begin)、substring(int beginindex, int endindex)
// 注意:字符串从零开始数
public void testSubstring(){
String se = new String("abcdefgh");
String sea = se.substring(3);
String seaa = se.substring(3,5);
System.out.println(sea);
System.out.println(seaa);
System.out.println("--------------------------------");
}
// 3、split(String s1)、split(String s2, int i)
public void testSplit(){
String s = "阳光:沙滩:海浪:仙人掌";
String[] results = s.split(":");
String[] result = s.split(":",3);
for(String each:results){
System.out.println(each);
}
System.out.println("------");
for(String each:result){
System.out.println(each);
}
System.out.println("-----------------------------------");
}
// 4、startsWith(String s1)、startsWith(String s2, int i)、endsWith(String s3)
public void testStartsendsWith(){
String str2 = new String("zyxwvuabcdef");
boolean bstart = str2.startsWith("z");
boolean bend = str2.endsWith("f");
if(bstart){
System.out.println("true");
}else{
System.out.println("false");
}
if(bend){
System.out.println("true");
}else{
System.out.println("false");
}
System.out.println("---------------------------------------");
}
// 5、replace(String sb, String st)、replaceAll(String sb, String st)
public void testReplaceAll(){
String sre = new String("cbadzyc");
String rsre = sre.replace("c","a");
String rasre = sre.replaceAll("c","b");
System.out.println(rsre);
System.out.println(rasre);
System.out.println("-----------------------------------------");
}
}