티스토리 뷰

it

String 메소드 / String함수

gams21 2023. 3. 28. 15:20
반응형

String 메소드 / String함수

public class StringTest {

public static void main(String[] args) {

//1. String 클래스이용

//. 리터럴==> 같은문자열은재사용한다.

String s = "hello"

String s2 = "hello"

// 배열로

char [] xxx = s.toCharArray();

System.out.println(s == s2); // true 주소값비교

System.out.println(s.equals(s2)); // true 문자열비교

//. new 이용==> 매번생성

String s3 = new String("hello");

String s4 = new String("hello");

System.out.println(s3 == s4); // false 주소값비교

System.out.println(s3.equals(s4)); // true 문자열비교

// 메소드정리

System.out.println("문자열길이: " + s.length());

// "hello"

char c = s.charAt(0); //h

System.out.println("특정문자얻기: " + c);

// 부분열

// "hello" ---> hel

String sub = s.substring(2);

System.out.println(sub); ////llo

String sub2 = s.substring(0, 3); // endIndex - 1

System.out.println(sub2);//hel

//문자열연결

String x = s.concat(" world");

System.out.println(x);//hello world

// 특정 문자위치(인덱스)

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

System.out.println(idx); // 일치하지않으면-1//1

//문자바꾸기

String x2 = s.replace("l", "w");

System.out.println(x2);//hewwo

// 대문자로바꾸기

String x3 = s.toUpperCase();

System.out.println(x3); //HELLO

//소문자로바꾸기

String d = "HellO"

System.out.println(d.toLowerCase()); //hello

//공백제거

String d2 ="Hello " //spacebar 3

System.out.println(d2.length() + "\t"+ d2); //8Hello

String d3 = d2.trim();

System.out.println(d3.length() +"\t" + d3); //5Hello

//특정구분자로문자열얻기

String k = "aaa/bbb/ccc"

String [] k2 = k.split("/"); // /구분자로문자열분리하기

for (String ss : k2) {

System.out.println(ss);//aaa bbb ccc

}

// 문자열로변경123-->"123" , 2.3F --> "2.3"

String a = String.valueOf(123);

String b = String.valueOf(true);

String c2 = String.valueOf(3.14);

System.out.println(a+"\t" + b +"\t" + c2); //123true3.14

}

}

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/05   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함