String: 문자열 - 변경되지 않는 문자열(문자 배열)
구조
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 = " ";
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);// ���
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]
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.