Notice
Recent Posts
Recent Comments
Link
«   2024/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
Archives
Today
Total
관리 메뉴

Far from it.

문자열에서 찾고자 하는 문자의 위치 찾기 본문

biz.note

문자열에서 찾고자 하는 문자의 위치 찾기

두유콩 2019. 8. 28. 09:55
1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) {
 
        String strNation;
        strNation = "Republic of Korea";
        
        int length = strNation.length();
        for(int i=0; i<length; i++) {
            char cA = strNation.charAt(i);
            if (cA == 'u') {
                System.out.println("u는 " +(i+1+"번째에 있습니다.");
            }
        }
cs

반복문에서 length를 활용하여 찾는 방법.

 

 

1
2
3
        int strNationIndex = strNation.indexOf("u");
        
        System.out.printf("u는 %d번째에 있습니다.", (strNationIndex+1));
cs

String.indexOF()를 활용하여 찾는 방법.

 

존재하는지 boolean 값을 return 받으려면 .Contains()를 활용

.matches => 정규식을 활용하여 찾는 방법

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.biz.classes;
 
public class String_04 {
 
    public static void main(String[] args) {
 
        String strNation="Republic of Korea";
        String strSearch="r";
        int intLength=strNation.length();
        
        String strAt;
        
        for (int i=0 ; i<intLength ; i++) {
            strAt=strNation.substring(i,i+1);
            if (strAt.equalsIgnoreCase(strSearch)) {
                System.out.println(strSearch + "은(는)" + i+ "번째에 위치해 있습니다.");
            }
        }
    }
 
}
cs
.substring을 이용하여 문자열에서 문자 찾는 방법

 

 

'biz.note' 카테고리의 다른 글

Scanner를 이용해서 char 변수 문자 입력 받기  (0) 2019.09.01
String  (0) 2019.08.27