728x90
substring
public String substring(int beginIndex) {
return substring(beginIndex, length());
}
charAt
public char charAt(int index) {
if (isLatin1()) {
return StringLatin1.charAt(value, index);
} else {
return StringUTF16.charAt(value, index);
}
}
replace
public int solution(String s) {
String[] arr = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for(int i = 0 ; i<arr.length ; i++){
s = s.replace(arr[i],Integer.toString(i));
}
return Integer.parseInt(s);
}
replaceAll
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
String str = "";
String[] digits = {"0","1","2","3","4","5","6","7","8","9"};
String[] alphabets = {"zero","one","two","three","four","five","six","seven","eight","nine"};
for(int i=0; i<10; i++){
str = str.replaceAll(alphabets[i],digits[i]);
}
// String phone_number;
phone_number.replaceAll(".(?=.{4})", "*");
- . (?=.{4}): 정규 표현식
- .
- 임의의 문자 하나를 의미
- (?=.{4})
- 긍정적 전방탐색(positive lookahead)으로, 현재 위치에서부터 뒤로 4글자가 더 있는 경우에만 일치
- 즉, 마지막 네 글자 앞의 문자들을 의미
- .
결과적으로, replaceAll 메서드는 phone_number 문자열에서 마지막 네 글자를 제외한 모든 문자를 *로 대체
int ↔️ String
String s = "";
int i =Integer.parseInt(s);
String str = String.valueOf(i);
char[] ↔️ String
String str1 = "abc";
char[] chs = str1.toCharArray();
String str2 = String.valueOf(chs);
728x90
'language > JAVA' 카테고리의 다른 글
| [JAVA] 객체지향 프로그래밍이란? (0) | 2024.06.23 |
|---|---|
| [JAVA] JVM(Java Virtual Machine) (2) | 2024.06.23 |
| [JAVA] 배열, 스트림 활용 (0) | 2024.06.22 |
| [JAVA] 제네릭(Generic), 제네릭 클래스, 제네릭 메서드, 와일드카드 (0) | 2024.06.22 |
| [JAVA][Exception] Map Duplicate key (0) | 2024.06.15 |