码迷,mamicode.com
首页 > 其他好文 > 详细

String定义与方法

时间:2015-10-24 11:26:56      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

//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);
  }

}

String定义与方法

标签:

原文地址:http://www.cnblogs.com/blogofcookie/p/4906346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!