진청우 2022. 10. 1. 13:18

선언하고 할당하기

String str = "Hello World!";

 

출력하기

String str = "This is string example."
System.out.println("ex1 : " + str);

 

 

길이 구하기 length()

String str = "AbcdEfghI01234";
System.out.println("The length of the str string is: " + str.length());

return 타입 int

 

 

대문자 / 소문자 변환

String str = "AbcDeFGhi";
System.out.println(str.toUpperCase());	// "ABCDEFGHI"
System.out.println(str.toLowerCase());	// "abcdefghi"

 

 

String에서 특정 문자열 찾기 indexOf()

String str = "Return the index of finding character.";
System.out.println(str.indexOf("index"));	// 11

존재하지 않으면 -1 리턴

index는 0부터 시작하며 공백도 센다.

 

 

String 연결 concat()

String s1 = "Very";
String s2 = "good";
System.out.println(s1 + " " + s2);	// "Very good"

String 오브젝트 끼리 합(+)으로 연결해줄 수 있다. 사이에 " "(space) 를 넣어 concatenation 해 주면 자연스럽다.

 

다른 변수끼리 더해주면 에러가 난다. String은 숫자만으로 이루어져 있어도 숫자형 변수가 아니다.

String n1 = "20";
String n2 = "30";
system.out.println(n1+n2);	// "2030"

concat() 메소드를 사용할 수도 있다.

System.out.println(s1.concat(" "+s2));

 

공백 안넣으면 붙어서 출력된다.