Substring / indexof
문자열 자르기 - substring()
java.lang.String 클래스의 substring() 메소드는
문자열의 특정 부분을 잘라내는 데 사용합니다.
Method Signature
substring() 메소드는 다음과 같이 2가지 형태로 사용할 수 있습니다.
- public String substring(int startIndex)
- public String substring(int startIndex, int endIndex)
substring(int startIndex)
startIndex부터 끝까지의 문자열을 리턴합니다.
public String substring(int startIndex)
substring() 메소드는
위 그림과 같이
substring() 메소드에 파라미터를 1개만 전달(startIndex)하면
문자열의 startIndex부터 끝까지의 문자열을 잘라서 리턴합니다.
(index는 0부터 시작합니다.)
substring(int startIndex, int endIndex)
startIndex(포함)부터 endIndex(불포함)까지의 문자열을 리턴합니다.
public String substring(int startIndex, int endIndex)
substring() 메소드는
위 그림과 같이
substring() 메소드에 2개의 파라미터를 전달하면(startIndex, endIndex)
startIndex부터 endIndex까지의 문자열을 잘라서 리턴합니다.
정확하게는 startIndex부터 lastIndex 전까지의 문자열을 잘라서 리턴합니다.
indexOf()
- indexOf(String str)
- indexOf(int ch)
- indexOf(int ch, int fromIndex)
- indexOf(String str, int fromIndex)
indexOf() 는 특정 문자나 문자열이 앞에서부터 처음 발견되는 인덱스를 반환하며
만약 찾지 못했을 경우 "-1"을 반환합니다.
사용법은 매우 간단하다
예제1) indexOf() 기본 사용법
public class IndexOfTest{
public static void main(String[] args){
String indexOfTestOne = "Hello world";
String indexOfTestTwo = " Hello world ";
System.out.println( indexOfTestOne.indexOf("o") ); // 4
System.out.println( indexOfTestOne.indexOf("x") ); // -1
System.out.println( indexOfTestOne.indexOf("o",5) ); // 7
System.out.println( indexOfTestTwo.indexOf("o") ); // 13
System.out.println( indexOfTestTwo.indexOf("el") ); // 10
}
}
.indexOf( "찾을 특정 문자" , "시작할 위치" ) 이런식으로 사용해 주면된다.
"시작할 위치" 같은경우는 생략이 가능하며 생략할 경우 0번째 즉 처음부터 찾기 시작한다.
3번째 예시에서보면 "o" 를 5번째 인덱스부터 찾기 시작하라고 했을때 Hello world 에서 두번째 o의 위치를 잡는것이다.
첫번째 o 의 위치가 4이기 때문에 그다음부터 찾기 시작하여 두번째 o의 인덱스를 반환하는 것이다.
그리고 공백 역시 하나하나 전부 위치를 잡고있기때문에
indexOfTestTwo 에서 " o " 를 찾을경우 앞에 빈칸을 전부 포함하여 13번째 위치에 있다는 결과가 반환되는 것이다.
"-1" 을 반환하는 이유는 위에서 설명 했다시피 찾지를 못했기 때문에 그런것이다.
이제 다음은 lastIndexOf() 에 대해 알아보도록 하자
lastIndexOf()
- lastIndexOf(String str)
- lastIndexOf(int ch)
- lastIndexOf(int ch, int fromIndex)
- lastIndexOf(String str, int fromIndex)
lastindexOf() 는 특정 문자나 문자열이 뒤에서부터 처음 발견되는 인덱스를 반환하며
만약 찾지 못했을 경우 "-1"을 반환합니다.
사용법은 indexOf 와 동일하다
예제1) lastIndexOf() 기본 사용법
public class IndexOfTest{
public static void main(String[] args){
String indexOfTestOne = "Hello world";
System.out.println( indexOfTestOne.lastIndexOf("o") ); // 7
System.out.println( indexOfTestOne.lastIndexOf("x") ); // -1
System.out.println( indexOfTestOne.lastIndexOf("o",5) ); // 4
}
}
여기서 신기한 점이 있는데
뒤에서부터 찾기 시작해서 찾았을경우 인덱스를 반환하는데
오른쪽에서 몇번째 위치하는지를 반환하는게 아니라 말그래도 인덱스
즉 왼쪽에서 몇번째 위치하는지를 인덱스로 반환한다.
이 점을 오해할 수 있으니 잘 알아두면 좋다
그래서 뒤에서는 분명 "o"의 위치가 3번째인데 정작 반환하는 값은 왼쪽에서의 index인 7을 반환한다.
참조 : https://hianna.tistory.com/534
[Java] 문자열 자르기 (substring)
Java에서 java.lang.String 클래스의 substring() 메소드를 사용하여 문자열을 자르는 방법을 소개합니다. 문자열 자르기 - substring() java.lang.String 클래스의 substring() 메소드는 문자열의 특정 부분을 잘라
hianna.tistory.com
참조 : https://mine-it-record.tistory.com/124
[JAVA] 자바_indexOf/lastIndexOf(특정 문자 위치 찾기)
- 특정 문자 위치 찾기 (indexOf/lastIndexOf) - indexOf() - indexOf(String str) - indexOf(int ch) - indexOf(int ch, int fromIndex) - indexOf(String str, int fromIndex) indexOf() 는 특정 문자나 문자열이 앞에서부터 처음 발견되는
mine-it-record.tistory.com