FreeHand

String 클래스 메서드 본문

Language/Java

String 클래스 메서드

Jinn 2023. 6. 28. 16:02

생성자

- String(String s) : 문자열 s를 갖는 String 인스턴스 생성

String s = new String("Hello");

s = "Hello"

 

- String(char[] arr) : 문자열 arr을 갖는 String 인스턴스 생성

char[] arr = { 'H', 'e', 'l', 'l', 'o' };

String s = new String(arr);

s = "Hello"

 

- String(StringBuffer buf) : StringBuffer 인스턴스가 갖고 있는 문자열과 같은 내용의 String 인스턴스 생성

StringBuffer buf = new StringBuffer("Hello");

String s = new String(buf);

s = "Hello"


메서드

- char charAt(int index) : index에 해당하는 문자 리턴

String s = "Hello";

char c = s.charAt(1);

c = 'e'

 

- int comapreTo(String str) : 문자열 str과 사전순으로 비교. 같으면 0, 사전순 이전이면 음수, 이후면 양수 리턴

int x = "aaa".compareTo("aaa");

int y = "aaa".compareTo("bbb");

int x = "bbb".compareTo("aaa");

x = 0, y = -1, z = 1

 

- String concat(String str) : 문자열 str을 뒤에 붙임

String s = "Hello";

String s2 = s.concat(" World");

s2 = "Hello World"

 

- boolean contains(CharSequence s) : 문자열 s가 포함되었는지 확인

String s = "Hello";

boolean b = s.contains("He");

b = true

 

- boolean endsWith(String s) : 문자열 s로 끝나는지 확인

- boolean startsWith(String s) : 문자열 s로 시작하는지 확인

String file = "Hello.txt";

boolean a = file.startsWith("Hello");

boolean b = file.endsWith("txt");

a = true, b = true

 

- boolean equals(Object obj) : 문자열 비교

String s = "Hello";

boolean b = s.equals("hello");

b = false

 

- boolean equalsIgnoreCase(String str) : 대소문자 구분 없이 문자열 비교

String s = "Hello";

boolean b = equalsIgnoreCase("hello");

b = true

 

- int indexOf(int ch) : ch에 해당하는 인덱스를 반환

String s = "Hello";

int idx1 = s.indexOf('o');

int idx2 = s.indexOf('a');

idx1 = 4, idx2 = -1 (존재하지 않음)

 

- int indexOf(int ch, int pos) : pos부터 확인하여 인덱스 반환

String s = "Hello";

int idx1 = s.indexOf('e', 0);

int idx2 = s.indexOf('e', 2);

idx1 = 1, idx2 = -1

 

- int indexOf(String str) : 문자열 str을 확인하여 인덱스 반환. 없으면 -1

String s = "abcde";

int idx = s.indexOf("c");

idx = 2

 

- int lastIndexOf(int ch || String str) : 오른쪽 끝에서부터 확인하여 인덱스 반환

String s = "java.lang.Object";

int idx1 = s.lastIndexOf('.');

int idx2 = s.lastIndexOf('.');

idx1 = 9, idx2 = 4

 

- int length() : 문자열의 길이

String s = "Hello";

int len = s.length();

len = 5

 

- String[] split(String regex) : 분리자 regex로 나누어 배열로 반환

String animals = "dog,cat,bear";

String[] arr = animals.split(",");

arr = ["dog", "cat", "bear"]

 

- String [] split(String regex, int limit) : limit 수 만큼 나눔

String animals = "dog,cat,bear";

String[] arr = animals.split(",", 2);

arr = ["dog", "cat,bear"]

 

- String substring(int begin), String substring(int begin, int end) : begin부터 end 직전까지 리턴

String str = "java.lang.Object";

String s1 = str.substring(10);

String s2 = str.substring(5, 9);

s1 = "Object", s2 = "lang"

 

- String toLowerCase() : 소문자로 변환

- String toUpperCase() : 대문자로 변환

String s = "Hello";

String s1 = s.toUpperCase();

String s2 = s.toLowerCase();

s1 = "HELLO", s2 = "hello"

 

- String trim() : 문자열의 양쪽 끝 공백을 없앰

String s = "    Hello World    ";

String s1 = s.trim();

s1 = "Hello World"

 

- static String valueOf(기본형 변수) : 문자열로 변환

String f = String.valueOf(10f);

java.util.Date d = new java.util.Date();

String date = String.valueOf(d);

f = "10.0", date = "Wed Jun 28 16:01:29 KST 2023

'Language > Java' 카테고리의 다른 글

Generics  (0) 2023.08.01
[기타] 문자열 붙이기(repeat)  (0) 2023.07.12
equals() 오버라이딩  (0) 2023.06.28
Iterator / ListIterator / Enumeration  (0) 2023.06.25
예외 처리  (0) 2023.06.25