레이블이 대소문자 확인인 게시물을 표시합니다. 모든 게시물 표시
레이블이 대소문자 확인인 게시물을 표시합니다. 모든 게시물 표시

2016년 11월 12일 토요일

자바 String 기본 예제 - 문자열 다루기

1. 문자열 포함 - contains
System.out.println("ABCD123".contains("CD"));



2. 시작문자열 - startsWith
System.out.println("https://www.google.com".startsWith("https://"));



3. 끝 문자열 - endsWith
System.out.println("image.jpg".endsWith(".jpg"));



4. 비교 - equals, equalsIgnoreCase
- 대소문자 구분
System.out.println("image.jpg".equals("image.jpg"));

- 대소문자 무시
System.out.println("image.jpg".equalsIgnoreCase("IMAGE.jpg"));



5. 문자열 자르기 - substring
- index 부터 자르기
System.out.println("www.google.com".substring(3));
> 결과 : google.com

- 구간 자르기
System.out.println("www.google.com".substring(4, 10));
> 결과 : google



6. 문자열 위치 찾기 - indexOf, lastIndexOf
- 왼쪽 기준에서 위치 찾기
System.out.println("www.google.com".indexOf("google"));

- 오른쪽 기준에서 위치 찾기
System.out.println("www.google.com".lastIndexOf(".com"));


예) 구간 자르기와 함께 쓰기
String value = "www.google.com";
int beginIndex = value.indexOf(".") + 1;
int endIndex = value.lastIndexOf(".");
System.out.println(value.substring(beginIndex, endIndex));

> 결과 : google
> +1을 하는 이유 : 10 11 12 13 m



7. 정규식 - matches
- 특정 문자가 포함되는 여부 판단
System.out.println("Qwer123".matches(".*[a-z][A-Z].*"));
> 대소문자 포함 여부 확인



8. 문자열 분리(배열) - split
System.out.println(Arrays.asList("www.google.com".split("\\.")));
> 결과 : [www, google, com]



9. 문자열 바꾸기 - replace, replaceAll
- 바꾸기
System.out.println("www.google.com".replace("www", "https://www"));

- 모두 바꾸기
System.out.println("w w w . g o o g l e . c o m".replaceAll(" ", ""));
> 공백 모두 제거



10. 대소문자 변경 - toLowerCase, toUpperCase
- 모두 소문자
System.out.println("www.google.com".toLowerCase());

- 모두 대문자
System.out.println("www.google.com".toUpperCase());