본문 바로가기
programming/java

[java] charAt(), indexOf()메소드 이용하는 프로그램

by 몽구스_ 2019. 10. 25.
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
 
public class IndexOfTest {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        
        String s;
        
        System.out.println("문장입력 : ");
        s = sc.next();
        
        int index = s.indexOf("A"); //A가 몇번째에 있는지 반환(정수형)
        char c = s.charAt(0); //0번째 문자를 반환(캐릭터형)
        
        if (index == -1)
            System.out.println("A는 없습니다.");
        else
            System.out.println("A는 " + (index + 1+ "번째에 있습니다 ");
        System.out.println("첫번째 문자는" + c + "입니다");
        sc.close();
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

charAt(), indexOf()의 차이점

 

charAt()

indexOf()

괄호안

int

char

반환

char

int

 

[이외 메소드]

toUpperCase() : 문자열을 다 대문자로 바꿔줌

compareTo(String another string) : 문자열이  같으면 0리턴, 이 문자열이 another string보다 사전에 먼저 나오면 음수리턴/더 나중에 나오면 양수리턴

equals() : 같으면 true, 다르면 false반환

 

 

댓글