Java06_문자열02

2 minute read



문자열탐색

1. indexOf()

//Hello World에서 'l'의 인덱스 반환하기. 
String str = "Hello World";
char ch = 'l';
System.out.println(str.indexOf(ch)); // 2
  • 찾을 문자열.indexOf(찾을문자);




문자열 변환

1. substring() : 인덱싱

String str = "hello world"; 
String substr = str.substring(1,6);// 1포함 5 전까지 
System.out.println(str + " --.substring(1,5)--> [" + substr+"]"); // hello world --.substring(1,5)--> [ello ] 
substr = str.substring(3); // 3부터 끝까지 
System.out.println(str + " --.substring(3)--> [" + substr+"]"); //hello world --.substring(3)--> [lo world]
  • 문자열.substring( start idx, end idx ); 일때, start idx부터 end idx -1까지 반환한다.

2. concat() : 합치기

// Hello와 !!!!!합치기 
String str1 = "Hello"; 
String str2 = "!!!!!"; 
String str = str1.concat(str2); 
System.out.println("concat(str1,str2) 결과 : " + str); 
// concat(str1,str2) 결과 : Hello!!!!!
  • 기준이 되는 문자열.concat(붙일 문자열);

3. replace() : 대체

String str = "hello world"; 
System.out.println("replace()전 결과 : "+str); 
// replace()전 결과 : hello world 

str = str.replace('l', 'r');  // l이면 전부 r로 바꿔준다. 
System.out.println("replace후() 결과 : "+str); 
// replace후() 결과 : herro worrd 

str = str.replace("rr", "l");  // 문자열로도 치환할 수 있다. 
System.out.println("replace후() 결과 : "+str); 
//replace후() 결과 : helo wold
  • 바꾸기 전의 문자열.replace( 바꾸고싶은 값(문자열/문자), 바꿀 값(문자열/문자) );

4. trim() : 시작과 끝부분 공백제거(중간은 제거 x)

String str = " hello "; 
System.out.println("trim()전 결과 : ["+str+"]의 길이 : "+str.length()); 
// trim()전 결과 : [ hello ]의 길이 : 11 

str = str.trim(); 
System.out.println("trim()후 결과 : ["+str+"]의 길이 : "+str.length()); 
// trim()후 결과 : [hello]의 길이 : 5
  • 제거할 문자열.trim();

5. toLowerCase( ) / toUpperCase( )

  • toLowerCase( ) : 모두 소문자로 반환
  • toUpperCase( ) : 모두 대문자로 반환
String str = "Hello World!"; 
System.out.println("str : "+ str); 
// str : Hello World! 

System.out.println("toLowerCase() : "+str.toLowerCase()); 
//toLowerCase() : hello world! 

System.out.println("toUpperCase() : "+str.toUpperCase()); 
// toUpperCase() : HELLO WORLD!

6. split( ) - 문자열을 쪼개서 배열에 넣어 반환

import java.util.Arrays; String str = "홍길동전:허균:조선시대"; 
String[] strArr = str.split(":"); 
System.out.println(str + " --split(\":\")--> "+Arrays.toString(strArr)); 
// 홍길동전:허균:조선시대 --split(":")--> [홍길동전, 허균, 조선시대]
  • 문자열.split(“기준이되는 부호”); -> 기준이되는 부호가 : 이면, :를 기준으로 3단어로 쪼개짐.
String ip = "192.168.5.203"; 
String[] ipArr = ip.split("."); 
System.out.println(ip+ " --split(\".\")--> "+Arrays.toString(ipArr)); 
// 192.168.5.203 --split(".")--> []
  • ipArr은 왜 빈리스트일까?
    • split( )에 입력한 “.”은 인식하지 못하기 때문에 따로 기호를 써서 표시해주어야 한다.
import java.util.Arrays; 

String ip = "192.168.5.203"; 
String[] ipArr = ip.split("."); 
ipArr = ip.split("\\."); 
System.out.println(ip+ " --split(\".\")--> "+Arrays.toString(ipArr)); 
// 192.168.5.203 --split(".")--> [192, 168, 5, 203]
  • ”.”이아닌 “\.”으로 써주어야 인식한다.




형의 변환

1. valueOf( )

int num = 123; 
String strNum = num+""; 
System.out.println("strNum+100 : "+strNum+100); 
// strNum+100 : 123100
  • int형에 +”“를 해주면 String형으로 바뀐다.
  • String타입인 strNum에 +100을 해주면 정수로 더해지지 않고 100이 문자처럼 뒤에 붙는다.
strNum = String.valueOf(num); // string형으로 바뀐다. 
System.out.println(strNum+10); // 

//boolean형처럼 보이지만 String으로 인식하게 된다. 
System.out.println(String.valueOf(true)); // true 
System.out.println(String.valueOf(true)+false); // truefalse