자바
문자열 자르기 substring
근즈리얼
2021. 2. 4. 15:38
728x90
자바를 사용하다 보면 문자열을 내가 원하는 길이만큼 자르고 싶을 때가 있다.
이럴 때 substring를 사용하면 효과적으로 문자열을 자를 수 있다.
1. 특정 index부터 뒷부분을 자르고 싶을 때 substring(int index)를 이용
String ex = "0123456789";
String tmp ="";
tmp = ex.substring(5);
System.out.println(tmp);//56789
2. 특정 부분을 자르고 싶을 때 substring(int beginindex, int endindex)를 이용
String ex = "0123456789";
String tmp ="";
tmp = ex.substring(0,5);
System.out.println(tmp);//01234
**주의**
1번같은 경우 index값을 포함한 index~끝부분이 tmp에 저장된다.
2번같은 경우 beginindex값을 포함하지만 endindex값을 포함하지않고 tmp값에 저장된다.
728x90