String定义与方法

//5种构造方法
public void Con(){
  String str = "sfaj";

  String str1 = new String("sfajdf");
  String str2 = new String();
  str2 = "sfaj";

  char[] cArr = {\'s\',\'f\',\'a\',\'d\',\'f\',\'j\'};
  String str3 = new String();

  String str4 = new String(cArr,0,2);
}

public void pra(){
  String str = new String("sfajdfjal;fjje23jkdjfa jdkf");
  //equals:相等
  System.out.println(str.equals("dddd"));
  //startsWith() && endsWith();
  System.out.println(str.startsWith("sfa"));
  System.out.println(str.endsWith("kf"));
  //compareTo()
  System.out.println("ac".compareTo("ad"));
  System.out.println("ac".compareTo("af"));
}
@Test
public void prac(){//
  String str = new String("sfajdfjal;fjje23jkdjfa jdkf");
  //indexOf();
  System.out.println(str.indexOf("s"));
  System.out.println(str.indexOf("f", 7));
  //lastIndexOf(str);lastIndexOf(str,int end)
  System.out.println(str.lastIndexOf("j"));
  System.out.println(str.lastIndexOf("j",10));
  //subString(int start);subString(int start,int end) start开始,end以前
  System.out.println(str.substring(20));
  System.out.println(str.substring(10,14));
}

public void pract(){
  String str = new String(" sfajdfjal;fjje23jkdjfa jdkf ");
  //replaceAll(str,str1);
  String str2 = str.replace("f", "哒哒哒");
  System.out.println(str2);
  //trim:去除前后的空格
  String str3 = str.trim();
  System.out.println(str3);
  //split();拆分
  String[] str4 = str.split("f");
  for(String s : str4){
    System.out.println(s);
  }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzysyz.html