IT박스

Java에서 문자열 부분 제거

itboxs 2020. 10. 28. 07:55
반응형

Java에서 문자열 부분 제거


한 문자에서 문자열의 일부를 제거하고 싶습니다.

소스 문자열 :

manchester united (with nice players)

대상 문자열 :

manchester united

이를 수행하는 방법에는 여러 가지가 있습니다. 교체하려는 문자열이 있으면 클래스 replace또는 replaceAll메서드를 사용할 수 있습니다 String. 하위 문자열을 교체하려는 경우 substringAPI를 사용하여 하위 문자열을 가져올 수 있습니다 .

예를 들면

String str = "manchester united (with nice players)";
System.out.println(str.replace("(with nice players)", ""));
int index = str.indexOf("(");
System.out.println(str.substring(0, index));

"()"내의 내용을 바꾸려면 다음을 사용할 수 있습니다.

int startIndex = str.indexOf("(");
int endIndex = str.indexOf(")");
String replacement = "I AM JUST A REPLACEMENT";
String toBeReplaced = str.substring(startIndex + 1, endIndex);
System.out.println(str.replace(toBeReplaced, replacement));

문자열 바꾸기

String s = "manchester united (with nice players)";
s = s.replace(" (with nice players)", "");

편집하다:

색인 별

s = s.substring(0, s.indexOf("(") - 1);

String.Replace () 사용 :

http://www.daniweb.com/software-development/java/threads/73139

예:

String original = "manchester united (with nice players)";
String newString = original.replace(" (with nice players)","");

처음에는 원래 문자열을 "("토큰이있는 문자열 배열로 분할하고 출력 배열의 위치 0에있는 문자열이 원하는 것입니다.

String[] output = originalString.split(" (");

String result = output[0];

StringBuilder를 사용 하면 다음과 같은 방법으로 바꿀 수 있습니다.

StringBuilder str = new StringBuilder("manchester united (with nice players)");
int startIdx = str.indexOf("(");
int endIdx = str.indexOf(")");
str.replace(++startIdx, endIdx, "");

originalString.replaceFirst("[(].*?[)]", "");

https://ideone.com/jsZhSC
replaceFirst()는 다음으로 대체 될 수 있습니다.replaceAll()


String 객체의 substring () 메서드를 사용해야합니다.

다음은 예제 코드입니다.

가정 : 여기서 첫 번째 괄호까지 문자열을 검색하고 싶다고 가정합니다.

String strTest = "manchester united(with nice players)";
/*Get the substring from the original string, with starting index 0, and ending index as position of th first parenthesis - 1 */
String strSub = strTest.subString(0,strTest.getIndex("(")-1);

Commons lang에서 StringUtils 사용

null 소스 문자열은 null을 반환합니다. 빈 ( "") 소스 문자열은 빈 문자열을 반환합니다. null 제거 문자열은 소스 문자열을 반환합니다. 빈 ( "") 제거 문자열은 소스 문자열을 반환합니다.

String str = StringUtils.remove("Test remove", "remove");
System.out.println(str);
//result will be "Test"

// Java program to remove a substring from a string
public class RemoveSubString {

    public static void main(String[] args) {
        String master = "1,2,3,4,5";
        String to_remove="3,";

        String new_string = master.replace(to_remove, "");
        // the above line replaces the t_remove string with blank string in master

        System.out.println(master);
        System.out.println(new_string);

    }
}

replace문자열을 수정 하는 사용할 수 있습니다 . 다음은 "("앞의 모든 것을 반환하고 선행 및 후행 공백을 모두 제거합니다. 문자열이 "("로 시작하면 그대로 둡니다.

str = "manchester united (with nice players)"
matched = str.match(/.*(?=\()/)
str.replace(matched[0].strip) if matched

"("뒤의 모든 항목을 제거해야하는 경우이를 시도하십시오. 괄호가 없으면 아무 작업도 수행하지 않습니다.

StringUtils.substringBefore(str, "(");

If there may be content after the end parentheses, try this.

String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")"); 

To remove end spaces, use str.trim()

Apache StringUtils functions are null-, empty-, and no match- safe

참고URL : https://stackoverflow.com/questions/8694984/remove-part-of-string-in-java

반응형