Java String 문자열 다뤄보는 문제풀이1

문자열을 다뤄보는 시간이다.


문자열에서 특정 문자열로 시작하는 코드

"https://example.com" 문자열에서 "https://" 으로 시작하는지 체크하는 코드

  • startsWith() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String url = "https://www.example.com";

        System.out.println("is https url? " + url.startsWith("https://"));
    }
}

is https url? true


배열 내부에 각각의 문자열의 길이를 알아보기

배열 내부에 과일이 적혀있다. 과일의 단어 길이를 알아보자.
그리고 마지막에는 구한 배열의 단어 길이의 총 합을 구하라

  • length() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String[] arr = { "Apple", "Banana", "Fruits", "Grapes", "Orange", "Pineapple", "Watermelon" };

        int sum = 0;
        int len = 0;
        for (String s : arr) {
            len = s.length();
            sum += len;
            System.out.println("This is Word Length ? " + s + ": " + len);
        }

        System.out.println("The sum is " + sum);
    }
}

This is Word Length ? Apple: 5
This is Word Length ? Banana: 6
This is Word Length ? Fruits: 6
This is Word Length ? Grapes: 6
This is Word Length ? Orange: 6
This is Word Length ? Pineapple: 9
This is Word Length ? Watermelon: 10
The sum is 48


시작되는지 특정 문자열 단어를 찾아 위치를 알아내기

문자열 file 이름에서 .rar 문자열이 어디서부터 시작하는지 위치를 표시하기

  • indexOf() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String file = "Hello_World_Java.part01.rar";
        System.out.println(file.indexOf(".rar"));
    }
}

23


특정 문자열 단어를 각각의 변수로 담아 출력하기

다음 주어진 문자열에서 "part01" 텍스트와 ".rar" 변수로 담아 출력한다.

  • substring() 메소드 사용
  • indexOf() 메소드 사용
  • lastIndexOf() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String file = "Hello_World_Java.part01.rar";
        String parts = file.substring(file.indexOf("part01"), file.lastIndexOf("."));
        String ext = file.substring(file.lastIndexOf("."));
        System.out.println(parts);
        System.out.println(ext);
    }
}

part01
.rar


다음 문자에서 특정 문자열의 갯수 출력하기

주어진 문자열에서 "." 점의 갯수가 몇개인지 출력한다.

  • indexOf() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String file = "He...llo_World_Java.part01.rar";
        String key = ".";
        int c = 0;
        int fromIndex = 0;

        while (file.indexOf(key, fromIndex) >= 0) {
            fromIndex = file.indexOf(key, fromIndex) + key.length();
            c++;
        }
        System.out.println("find word(.) count: " + c);
    }
}

find word(.) count: 5


앞뒤 공백 제거하기

주어진 문장의 앞 뒤에 공백이 있다. 제거해서 출력한다.

  • trim() 또는 strip() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String file = "  Hello World  ";
        System.out.println(file.trim());
    }
}

Hello World


문자열 바꾸기

"Hello" 단어를 "Java" 단어로 바꾸기

  • replace() 사용하기
public class Main {
    public static void main(String[] args) {
        String file = "Hello World!";
        System.out.println(file.replace("Hello", "Java"));
    }
}

Java World!


문자열에서 특정 단어 분리하기

URL 문자열에서 프로토콜 https 과 도메인명 분리해서 출력하기

  • split() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String file = "https://www.kiioio.com";
        String[] arr = file.trim().split("://");
        for (String s : arr) {
            System.out.println(s);
        }
    }
}

https
www.kiioio.com


과일 배열을 특수문자 기준으로 분리하고 특수문자 바꾸기

  • split() 메소드 사용
  • join() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String fruits = "apple,banana,orange";
        String[] arr = fruits.split(",");

        for (String s : arr) {
            System.out.println(s);
        }
        
        System.out.println(String.join("|", arr));
    }
}

apple
banana
orange
apple|banana|orange


문장을 뒤집어서 출력하기

문장을 뒤집어서 사용하도록 한다. 일반 String 타입에는 reserve 메소드가 없으므로 StringBuilder 클래스 객체로 가져와 사용한다.

  • StringBuilder 클래스 사용
  • append() 메소드 사용
  • reserve() 메소드 사용
  • toString() 메소드 사용
public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        String rev = new StringBuilder().append(str).reverse().toString();
        System.out.println(rev);
    }
}

dlroW olleH