JAVA API(String 클래스 주요메소드)

·2022년 11월 6일
0

JAVA객체지향

목록 보기
2/4
  • 기본적으로 java.lang에 소속되어 있는 모든 클래스들은 import하지 않아도 불러옴
  • <API문서보는 법>
    [step1] java api 11구글에 검색해서 들어간다.
    [step2]원하는 클래스.객체.메소드를 찾는다(끝)

String클래스 주요 메소드 몇가지

1. public String(char[] value): 객체 생성시 배열을 생성한 뒤 문자열로 출력

char[] value = {'A','B','C'}
String st = new String(value);

→char은 문자 하나. char배열로 먼저 만들어 준 뒤
→String으로 반환하삼…

2.toLowerCase(): 문자열을 소문자로 바꾸어주는 메소드

Syste.out.println(st+"를 소문자로=> "+st.toLowerCase());

→출력: ABC를 소문자로⇒ abc

3. public int length(): 문자열 길이 반환.. 이건 뭐 하도 많이 했으니 패스

4. public char charAt(int index): n번째 문자열 추출

String st = "AsciaEmetselch");
char ch1 = st.charAt(2);
문자하나 참조변수 = 문자열중 두번째 문자
System.out.println("ch1: "+ch1);

→출력⇒ ch1: s
→얘를 사용하고 싶으면 무조건 String 타입으로 출력해야함

5. public int indexOf(String str): 해당 문자가 몇번째에 위치해 있는지

String st = "AscianEmetselch";
int a = st.indexOf("E");
System.out.println("E의 위치: "+a);

→출력⇒ E의 위치: 7
→매개변수 안의 문자가 문자열의 몇번째에인
→매개변수의 문자가 없으면 -1을 반환함

6. public String[] split(String regex): 어떤 것을 기준삼아 문자열을 쪼개주는 메소드

String st="피카츄,라이츄,파이리,꼬부기,버터풀,야도란,피존투,또가스";
String[] s1 = st.split(",")
//
for(int i=0;i<s1.length;i++){
	System.out.println(s1[i]);
}

→쪼개면 배열이 되므로 배열로 출력한다.(for문으로 출력)

7. substring(int beginIndex, int EndIndex): 문자열중에 특정범위만 출력

8.public static int parseInt(String s)throws NumberFormatException: String문자열을 int 값으로..

String a ="100";
int n1 = Integer.parseInt(a);
System.out.println(n1);

→출력⇒ 100

9.public static String toBinaryString(int i): String 문자열을 이진수로 출력

String s = Integer.toBinaryString(n1);
System.out.println(n1+"의 이진수: "+s);

→출력⇒100의 이진수: 1100100
→메소드를 보면 return값이 String이기 때문에 String클래스로 객체를 만들어준다.

10.public byte[] getBytes(): String 문자열을 byte배열로 출력

String s = "Hello";
byte[] a = s.getBytes(); 
//숫자로 변환되기 때문에 char변환 해줌
for(int i =0;i<a.length;i++){
	syso((char)a[i]);
}

→출력⇒Hello

11.public char[] toCharArray(): String을 문자배열로 나누어서 출력

String s = "안녕하세요";
char[] c = s.toCharArray();
syso(c[1]);

→출력⇒안

profile
웹개발입문자

0개의 댓글