day(12)The Character String Operation(字符串操作)

The Basic Class Library

There is no need to recite those functions.
But we still should know methods in those class and function name.

String(字符串)

String & StringBuilder
1.'String' is an immutable types(不可变类型).We can' t change its defined character strings directedly
2.StringBuilder is mutable type

Create a character string

String class is modified by final it can't be inherited.
boolean equals(Object var1)-contents
int compareTo(int var1); 1:a>b 0:a=b -1:a<b

publc class MyClass{
public void main(String[] args){
// The final character string(常量字符串)
String country = "“中国”;
String city = "重庆";

//Use constructor to create
String detail = new String();
String detail = new String(s:"中国");

System.out.println(detail == detail1)
//false
// ' == ' This compares the object itself.
Syetem.out.println(country.equals(detail1));
//true
//' equals ' This compare the contents.
System.out.println(country.compareTo("中国"));
//0
System.out.println(country.compareTo("中国1"));
//-1
System.out.println(country.compareTo("中"));
//1
  }
}

![_P{3G26NYQQ7Q5L)1(5J}D.png

{@R}FNI{I1IIN8XFNA8%NXH.png

char charAt(int var1); -- This is corresponding to a 'char' array.
boolean isEmpty()
boolean startsWith(); -Judge whether this character string is start with certain string.
indexOf(int var1,int var2)

public class MyClass{
 main(){
  char[] name = {'j','a','c','k'};
  String s_name = new String(name);
  System.out.println(s_name);
//jack 
//length
for(int i = 0; i < s_name.length; i++){
    System.out.print(s_name.charAt(i));
//jack
if (s_name.isEmpty()){
      break;
      } 
    }
  }
}

startsWith(); & endsWith(); & indexOf(s:,i:); & substring(i:,i:); & replaceAll(s:,s:); & splist(s:);(An char array) & trim();

indexOf(); -- Get the index of certain string
substring();-- Get certain string starts from the first 'i'.
replaceAll(s:,s:); --Repalce certain string with another string.The original string won't change.
split(s:"");-- Divide the whole string with certain character
trim();-- Get rid of the backspace exists in string

public class MyClass{
  mian(){
  String url = "http://http://www.baidu.com";
  System.out.println(url.startsWith("http");
//ture
  System.out.println(url.startsWith(s:"http",i:7);
//true

  String pic = "http://www.baidu.com/image/1.jpg";
  String pic1 = "http://www.baidu.com/image/1.jpeg";
  String pic2 = "http://www.baidu.com/image/1.png";
  if(pic.endsWith("jpg") || pic.endsWith("jpeg") || pic.endsWith("png"){
    System.out.println("图片");
  }else{
    System.out.println("不是图片”);  

    String pic2 = "http://www.baidu.www.com/image/1.png";
    System.out.println(pic2.indexOf("baidu"));
    System.out.println(pic2.indexOf(s:"www));
//7
    System.out.println(pic2.indexOf(s:"www",i:8));
//17

System.out.println(pic2.substring(4));
// //www.baidu.www.com/image/1.png

System.out.println(pic2.substring(4,7));
// ://w

System.out.println(s:"baidu",sl:"google"));
//http://www.google.www.com/image/1.png
// The original pic2 won't change.

String[] split = pic2.split(s:"/");
for(String comp: split){
    System.out.println(comp);
//http:
//
//www.google.www.com
//image
//1.png
      }

String pic4 = "http://www.baidu.com/image/1.jpg"      ;
System.out.print(pic4.trim());
//http://www.baidu.com/image/1.jpg
    }
}

StringBuilder

append(s:"");-- Add a character string at the end of last character string.
replace(i:,i1:,s:"");
lastIndexOf(s:"");
delete(i:,i1:);
insert

public class MyClass{
  main(){
//StringBuilder
    StringBuilder test = new StringBuilder();
    test.append("my name is ");
    test.append("jack");
//my name is jack
    test.replace(i:0,i1:10,"merry");
//merry jack
    int start = test.lastIndexOf(s:"jack");
    test.replace(start,i1:start+"jack".length(),s:"merry");
    System.out.println(test);
//my name is merry

test.delete(i:0,,i1:2);
//name is merry

test.insert(i:0,s:"my");
//my name is merry
  }
}

Collection

Set List Map

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容