String 클래스

Jan 01, 2024
String 클래스
Contents
 
문자열 길이 확인하기: length()
String str = "Hello, World!"; int length = str.length(); System.out.println("문자열 길이: " + length);// 출력: 13
문자열 비교하기: equals() *****
String str1 = "Hello"; String str2 = "Hello"; boolean isEqual = str1.equals(str2); System.out.println("문자열 비교 결과: " + isEqual);// 출력: true
문자열 결합하기: concat()
String str1 = "Hello"; String str2 = "World!"; String result = str1.concat(str2); System.out.println("문자열 결합 결과: " + result);// 출력: HelloWorld!
특정 문자열 포함 여부 확인하기: contains()
String str = "Hello, World!"; boolean containsHello = str.contains("Hello"); System.out.println("문자열 포함 여부: " + containsHello);// 출력: true
문자열 분리하기: split()
String str = "Java is a programming language"; String[] words = str.split(" "); for (String word : words) { System.out.println(word); } // 출력:// Java// is// a// programming// language
문자열 대체하기: replace()
String str = "Hello, World!"; String replacedStr = str.replace("World", "Java"); System.out.println("대체된 문자열: " + replacedStr);// 출력: Hello, Java!
문자열 추출하기: substring()
String str = "Hello, World!"; String subStr = str.substring(7); System.out.println("추출된 문자열: " + subStr);// 출력: World!
 

예제

package ex08.example; public class StringEx02 { public static void main(String[] args) { // 문서 // ? 뒤를 쿼리스트링이라고 한다. // 쿼리스트링은 키=값 형태의 데이터의 집합이다. // 키=값이 여러개일때는 &로 구분한다.\ // 어떤 URL을 받아도 똑같은 값이 나오게 만들기 String url = "http://www.naver.com?username=ssar&password=1234"; // username값과, password값 찾아보기 String[] spc = url.split("\\?"); for (String word : spc) { // System.out.println(word); } String a = spc[1]; String []b = spc[1].split("="); System.out.println(b[2]);//password 값 String []c = b[1].split("&"); System.out.println(c[0]); // username 값 //배열의 인덱스는 배열에 넣을 수 있지만 배열은 배열에 넣지 못한다. } }
결과
notion image
 
 
package ex08.example; class ContractInfo { //계약정보 private final String addr; private int money; private String name; private String tel; private String email; public ContractInfo(int money, String addr, String name, String tel, String email) { this.money = money; this.addr = addr; this.name = name; this.tel = tel; this.email = email; } void print() { System.out.println(money+":"+addr+":"+name+":"+tel+":"+email); } // 100:서울분당:이창호:01033338888:hello@nate.com @Override public String toString() { // tostring만 return = System.out.print의 대체다 return money+":"+addr+":"+name+":"+tel+":"+email; } public int getMoney() { return money; } public String getAddr() { return addr; } public String getName() { return name; } public String getTel() { return tel; } public String getEmail() { return email; } } public class StringEx03 { public static void main(String[] args) { // 문서 데이터는 : 으로 구분된다. // 0 : 계약금 // 1 : 주소 // 2 : 이름 // 3 : 전화번호 // 4 : 이메일 // 100:서울분당:이창호:01033338888:hello@nate.com ContractInfo ci = new ContractInfo(100, "서울분당", "이창호", "01033338888", "hello@nate.com"); // System.out.println(ci); String []a = ci.toString().split(":"); for (String word : a){ System.out.println(word); } } }
결과
notion image
Share article
RSSPowered by inblog