programing

지정된 문자열에서 서브스트링을 삭제하려면 어떻게 해야 합니까?

nicegoodjob 2023. 1. 26. 11:33
반응형

지정된 문자열에서 서브스트링을 삭제하려면 어떻게 해야 합니까?

주어진 서브스트링을 쉽게 제거할 수 있는 방법이 있습니까?String자바어?

예:"Hello World!", 삭제"o""Hell Wrld!"

다음과 같이 간단하게 사용할 수 있습니다.

String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");

String Buffer를 사용할 수 있습니다.

StringBuffer text = new StringBuffer("Hello World");
text.replace( StartIndex ,EndIndex ,String);
replace('regex', 'replacement');
replaceAll('regex', 'replacement');

이 예에서는

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

Apache String Utils를 확인합니다.

  • static String replace(String text, String searchString, String replacement)다른 문자열 내의 모든 문자열을 바꿉니다.
  • static String replace(String text, String searchString, String replacement, int max)검색 String의 첫 번째 최대값에 대해 String을 큰 String 내의 다른 String으로 바꿉니다.
  • static String replaceChars(String str, char searchChar, char replaceChar)문자열 내의 모든 문자를 다른 문자로 바꿉니다.
  • static String replaceChars(String str, String searchChars, String replaceChars)String 내의 여러 문자를 한 번에 바꿉니다.
  • static String replaceEach(String text, String[] searchList, String[] replacementList)다른 문자열 내의 모든 문자열을 바꿉니다.
  • static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)다른 문자열 내의 모든 문자열을 바꿉니다.
  • static String replaceOnce(String text, String searchString, String replacement)String을 큰 String 내의 다른 String으로 한 번 바꿉니다.
  • static String replacePattern(String source, String regex, String replacement)지정된 정규 표현과 일치하는 소스 문자열의 각 하위 문자열을 패턴을 사용하여 지정된 대체 문자열로 바꿉니다.DOTALL 옵션

이거면 되겠네요.

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

또는 를 사용할 수 있습니다.

String no_o = hi.replace("o", "");

당신은 그것을 봐야 한다.StringBuilder/StringBuffer지정된 오프셋으로 문자를 삭제, 삽입, 대체할 수 있습니다.

시작 및 종료 인덱스를 알고 있는 경우 다음을 사용할 수 있습니다.

string = string.substring(0, start_index) + string.substring(end_index, string.length());
replaceAll(String regex, String replacement)

위의 방법이 답을 얻는 데 도움이 될 것이다.

String check = "Hello World";
check = check.replaceAll("o","");

Substring은 기존 문자열로 대체하기 위해서도 사용할 수 있습니다.

var str = "abc awwwa";
var Index = str.indexOf('awwwa');
str = str.substring(0, Index);

guava의 CharMatcher.removeFrom 함수를 사용할 수도 있습니다.

예:

 String s = CharMatcher.is('a').removeFrom("bazaar");

다음은 지정된 문자열에서 모든 하위 문자열을 삭제하는 구현입니다.

public static String deleteAll(String str, String pattern)
{
    for(int index = isSubstring(str, pattern); index != -1; index = isSubstring(str, pattern))
        str = deleteSubstring(str, pattern, index);

    return str;
}

public static String deleteSubstring(String str, String pattern, int index)
{
    int start_index = index;
    int end_index = start_index + pattern.length() - 1;
    int dest_index = 0;
    char[] result = new char[str.length()];


    for(int i = 0; i< str.length() - 1; i++)
        if(i < start_index || i > end_index)
            result[dest_index++] = str.charAt(i);

    return new String(result, 0, dest_index + 1);
}

isSubstring() 메서드의 실장은 다음과 같습니다.

private static void replaceChar() {
    String str = "hello world";
    final String[] res = Arrays.stream(str.split(""))
            .filter(s -> !s.equalsIgnoreCase("o"))
            .toArray(String[]::new);
    System.out.println(String.join("", res));
}

문자를 필터링할 수 있는 복잡한 논리를 가지고 있다면replace().

사용할 수 있습니다.

String helloWorld = "Hello World";
String target = "e";
String replacement = "";
String replacedString = helloWorld.replace(target, replacement);

The answer is = Hllo World

또는 regex를 사용하여

String original = "Java is one of best languages. OOP can be used in Java";
String regexTarget = "\\bJava\\b";
String replacedWord = original.replaceAll(regexTarget, "Python");

The answer is = Python is one of best languages. OOP can be used in Python

@DwB answer 외에 String Utils를 사용할 수도 있습니다. remove:

String hello = "hello world";
String hellYeah = StringUtils.remove(hello, "o");

또는removeIgnoreCase:

String hello = "hellO world";
String hellYeah = StringUtils.remove(hello, "o");

언급URL : https://stackoverflow.com/questions/7775364/how-can-i-remove-a-substring-from-a-given-string

반응형