갱스터하우스

자바 split(".") 안 됨 본문

코테 문제/알고리즘 공부

자바 split(".") 안 됨

승갱 2024. 1. 3. 00:03

백준 20291번을 풀다 "."을 기준으로 문자열을 슬라이싱하기위해 split을 썼는데 슬라이싱이 안 되었다.

// 변경 이전
String tmp = br.readLine().split(".")[1];

 

 

알고보니 split 메서드는 파라미터로 regx, 즉 정규표현식을 입력받는다.

정규표현식에서 "."의 의미는 임의의 한 문자를 의미한다.

그러므로 "." 의미로 쓰려면 "[.]" 또는 "\\." 로 작성해야한다.

// 변경 이후
String tmp = br.readLine().split("\\.")[1];

 

 

 

참고

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

 

String (Java Platform SE 8 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com