String: 문자열 - 변경되지 않는 문자열(문자 배열)

4923 단어

구조

  • 문자열-> 문자 배열로 포장된 대상
  • 	char[] cs = {'h','e','l','l','o'};
    	System.out.println(cs);//hello
    

  • 	String str = "hello1";
    	str = "hi";
    	System.out.println(str);//hi
    
  • 구조기
  • 	String s1 = new String(); // ""
    	s1 = new String(" "); // s1 = " "
    	System.out.println(s1.toString());// 
    
  • 구조기
  • 	s1 = new String(new char[] {'h','e','l','l','o'});// hello
    

    2. 인코딩 문제


    인코딩 문제 JVM 문자 세트 유니코드\u4e2d 운영 체제: GBK (1자=2바이트) UTF-8 (1자=3바이트)
    String str = " ";
    
  • 문자-> 바이트:str.getBytes 디코딩()
  • 	byte[] bsUTF = str.getBytes();// (UTF-8)
    	System.out.println(Arrays.toString(bsUTF));// [-28, -67, -96, -27, -91, -67]
    	byte[] bsGBK = str.getBytes("GBK");// 
    	System.out.println(Arrays.toString(bsGBK));// [-60, -29, -70, -61]
    
  • 바이트-> 문자: 인코딩
  • 	String s1 = new String(bsUTF);// 
    	System.out.println(s1);//  
    	String s2 = new String(bsUTF,"GBK");// 
    	System.out.println(s2);//  ソ
    	String s3 = new String(bsGBK,"UTF-8");
    	System.out.println(s3);// ���
    
  • 부호화 문제 해결, 예를 들어 s2
  • 	String s4 = new String(s2.getBytes("GBK"),"UTF-8");
    	System.out.println(s4);//  
    

    3. 비교가 같은지

    	String s1 = new String("hello");
    	String s2 = new String("hello");
    	System.out.println(s1 == s2); // false
    	//  : , , 
    	String s3 = "hello";
    	String s4 = "hello";
    	System.out.println(s3 == s4); // true
    
    	String s6 = "he";
    	String s7 = "hello";
    	// +  ->  
    	String s8 = "he" + "llo";
    	// +  ->  
    	String s9 = s6 + "llo";
    	System.out.println(s7 == s8); // true
    	System.out.println(s7 == s9); // false
    

    방법

    	String str = "he+llo";
    	//   -   length
    	int length = str.length();
    	//   toCharArray
    	char[] cs = str.toCharArray();
    	//   charAt
    	char c = str.charAt(2);
    	//   equals
    	String s1 = "gbk";
    	String s2 = "GBK";
    	boolean b1 = s1.equals(s2);
    	//   toUpperCase
    	s1 = s1.toUpperCase();
    	//   toLowerCase
    	s2 = s2.toLowerCase();
    	//   equalsIgnoreCase
    	boolean b2 = s1.equalsIgnoreCase(s2);
    
    	String s1 = "hello";
    	//   concat
    	s1 = s1.concat(",.");
    	// s1  contains
    	boolean isCon = s1.contains("ll");
    	//   endsWith
    	boolean isEnds = s1.endsWith("@126.com");
    	//   startsWith
    	boolean isStarts = s1.startsWith("+86");
    	//  2, 
    	isStarts = s1.startsWith("ll", 2);
    
    	String s1 = "how are you, how old are you";
    	//  w  indexOf
    	int index = s1.indexOf('w'); // 2
    	//  
    	index = s1.indexOf("are"); // 4
    	//  , 
    	index = s1.indexOf("are", 10);// 21
    	//  w  lastIndexOf
    	index = s1.lastIndexOf('w');
    	//  , 'w' 
    	index = s1.lastIndexOf('w', 10);
    
    	String str = " ";
    	//  0 isEmpty
    	boolean isEmpty = str.isEmpty();
    	//   replace
    	String s1 = "qnmlgb,  ";
    	s1 = s1.replace("qnmlgb", "***");
    	//    \t 
    trim String s2 = " hz_liuzb \t
    \r"; s2 = s2.trim(); // , substring String s3 = "[email protected]"; String s4 = s3.substring(0); s4 = s3.substring(0, 8); s4 = s3.substring(0, s3.lastIndexOf('@'));

    5. 정규 표현식 [선택할 수 있는 옵션] - 한 문자


    ?
    0 또는 1 번
    *
    0번 이상
    +
    1 또는 여러 번
    {n}
    꼭 n번
    {n,}
    최소 n회
    {n,m}
    적어도 n번은 넘지 않지만 m번은 넘지 않는다
    .
    모든 문자
    \w
    단어 문자 [a-zA-Z0-9]
    \d
    숫자.
    \s
    공백: [\t\x0B\r]
    [abc]
    a, b 또는 c(단순 클래스)
    [^abc]
    a, b 또는 c를 제외한 모든 문자
    [a-zA-Z]
    A-z 또는 A-Z 사이의 문자(범위)
    \\
    백슬래시 문자("\"
    예1: 전화번호 11자리, 첫 번째 자리 1, 뒤 10자리 마음대로 숫자
    	String str = "22342343244";
    	boolean isMat = str.matches("1[0-9]{10}");// false
    

    예2:163 메일박스 [a-zA-Z]\w{5,11}@163.com
    	str = "[email protected]";
    	String regex = "[a-zA-Z]\\w{5,11}@163\\.com";
    	System.out.println(str.matches(regex));// true
    

    예3: 교체, 차단
    String str = " qnmlgb, cnm,  nc,  tm ";
    String regex = "(qnmlgb)|(cnm)|(nc)|(tm)";
    str = str.replaceAll(regex, "**");//  **, **,  **,  ** 
    

    예4:
    String str = "abc123der52ersdf45sdf34sewr76";
    		// 1. *, *   \d+
    		String newStr = str.replaceAll("\\d+", "*");
    		// 2. ( ), ,    [a-zA-Z]+
    		String[] ss = str.split("[a-zA-Z]+");// [ , 123, 52, 45, 34, 76]
    

    좋은 웹페이지 즐겨찾기