Chcę usunąć część ciągu z jednego znaku, czyli:
Ciąg źródłowy:
manchester united (with nice players)
Ciąg docelowy:
manchester united
Odpowiedzi:
Można to zrobić na wiele sposobów. Jeśli masz ciąg, który chcesz zastąpić można użyć replacelub replaceAllmetod Stringklasy. Jeśli chcesz zamienić podciąg, możesz go uzyskać za pomocą substringinterfejsu API.
Na przykład
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));
Aby zamienić zawartość w „()”, możesz użyć:
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 toBeReplaced = str.substring(startIndex , endIndex + 1);
Zastąp ciąg
String s = "manchester united (with nice players)";
s = s.replace(" (with nice players)", "");
Edytować:
Według indeksu
s = s.substring(0, s.indexOf("(") - 1);
s = s.replace(...)
Użyj String.Replace ():
http://www.daniweb.com/software-development/java/threads/73139
Przykład:
String original = "manchester united (with nice players)";
String newString = original.replace(" (with nice players)","");
originalString.replaceFirst("[(].*?[)]", "");
https://ideone.com/jsZhSC
replaceFirst()można zastąpićreplaceAll()
Używając StringBuilder , możesz zastąpić następujący sposób.
StringBuilder str = new StringBuilder("manchester united (with nice players)");
int startIdx = str.indexOf("(");
int endIdx = str.indexOf(")");
str.replace(++startIdx, endIdx, "");
Najpierw podzieliłbym oryginalny ciąg na tablicę String z tokenem „(”, a String na pozycji 0 tablicy wyjściowej jest tym, co chciałbyś mieć.
String[] output = originalString.split(" (");
String result = output[0];
Należy użyć metody substring () obiektu String.
Oto przykładowy kod:
Założenie: Zakładam tutaj, że chcesz pobrać ciąg do pierwszego nawiasu
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);
Używanie StringUtils z commons lang
Łańcuch źródłowy o wartości null zwróci wartość null. Pusty („”) ciąg źródłowy zwróci pusty ciąg. Łańcuch do usunięcia o wartości null zwróci ciąg źródłowy. Pusty („”) ciąg usuwania zwróci ciąg źródłowy.
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);
}
}
Jeśli chcesz tylko usunąć wszystko po „(”, spróbuj tego. Nie robi nic, jeśli nie ma nawiasów).
StringUtils.substringBefore(str, "(");
Jeśli po nawiasach końcowych może znajdować się treść, spróbuj tego.
String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")");
Aby usunąć spacje końcowe, użyj str.trim()
Funkcje Apache StringUtils są null-, empty- i no match-safe
Możesz użyć, replaceaby naprawić swój ciąg. Poniższe polecenie zwróci wszystko przed „(”, a także usunie wszystkie początkowe i końcowe białe znaki. Jeśli ciąg znaków zaczyna się od „(”, pozostawi go bez zmian.
str = "manchester united (with nice players)"
matched = str.match(/.*(?=\()/)
str.replace(matched[0].strip) if matched
Jeśli usuwasz określony ciąg z końca, użyj removeSuffix ( Dokumentacja )
var text = "one(two"
text = text.removeSuffix("(two") // "one"
Jeśli sufiks nie istnieje w ciągu, po prostu zwraca oryginał
var text = "one(three"
text = text.removeSuffix("(two") // "one(three"
Jeśli chcesz usunąć po znaku, użyj
// Each results in "one"
text = text.replaceAfter("(", "").dropLast(1) // You should check char is present before `dropLast`
// or
text = text.removeRange(text.indexOf("("), text.length)
// or
text = text.replaceRange(text.indexOf("("), text.length, "")
Można również sprawdzić removePrefix, removeRange, removeSurrounding, ireplaceAfterLast które są podobne
Pełna lista znajduje się tutaj: ( Dokumentacja )