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);
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
'IT박스' 카테고리의 다른 글
| angular-cli 서버-API 요청을 다른 서버로 프록시하는 방법은 무엇입니까? (0) | 2020.10.28 |
|---|---|
| iPhone에서 빈 영역을 터치하면 키보드를 숨기는 방법 (0) | 2020.10.28 |
| NSURLConnection에 대한 Xcode 4 경고 "표현 결과가 사용되지 않음" (0) | 2020.10.28 |
| Angular 2 경로가 존재하지 않는 경우 404 또는 다른 경로로 리디렉션하는 방법 (0) | 2020.10.28 |
| LLDB의 배열보기 : Xcode 4.1의 GDB '@'연산자와 동일 (0) | 2020.10.28 |