티스토리 뷰
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
}
}
'it' 카테고리의 다른 글
| 데이터베이스 테이블 구조 설계 (3가지방법) (0) | 2023.03.30 |
|---|---|
| 서버단 jsp (정보입력/ 정보조회) (0) | 2023.03.29 |
| 스프링 개발환경구축 (6-3) SVN 용어 요약 (0) | 2023.03.27 |
| INDEX적용 - 인덱스를 사용하지 못하는 경우 (0) | 2023.03.26 |
| 컴퓨터 프로그램 (0) | 2023.03.25 |
