我想从一个字符中删除一部分字符串,即:
源字符串:
manchester united (with nice players)
目标字符串:
manchester united
答案 0 :(得分:133)
有多种方法可以做到这一点。如果您有要替换的字符串,则可以使用replace
类的replaceAll
或String
方法。如果您要替换子字符串,可以使用substring
API获取子字符串。
例如
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));
答案 1 :(得分:27)
字符串替换
String s = "manchester united (with nice players)";
s = s.replace(" (with nice players)", "");
修改强>
按索引
s = s.substring(0, s.indexOf("(") - 1);
答案 2 :(得分:17)
使用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)","");
答案 3 :(得分:6)
我首先将原始字符串拆分为带有标记“(”的字符串数组,并且输出数组的位置0处的字符串是您想要的字符。
String[] output = originalString.split(" (");
String result = output[0];
答案 4 :(得分:6)
使用 StringBuilder ,您可以替换以下方式。
StringBuilder str = new StringBuilder("manchester united (with nice players)");
int startIdx = str.indexOf("(");
int endIdx = str.indexOf(")");
str.replace(++startIdx, endIdx, "");
答案 5 :(得分:6)
Option Explicit
Function CleanStopWords(S As String, StopWords As Range)
Dim RE As Object
Dim SW() As String
Dim C As Range
Dim I As Long
ReDim SW(1 To StopWords.Count)
For I = 1 To StopWords.Count
SW(I) = StopWords(I)
Next I
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.ignorecase = True
'create pattern using the StopWords
.Pattern = "\b(?:" & Join(SW, "|") & ")\b\s*"
CleanStopWords = .Replace(S, "")
End With
End Function
{{3}}
originalString.replaceFirst("[(].*?[)]", "");
可以替换为replaceFirst()
答案 6 :(得分:5)
您应该使用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);
答案 7 :(得分:2)
Using StringUtils from commons lang
空源字符串将返回null。空(“”)源字符串将返回空字符串。 null remove字符串将返回源字符串。空(“”)删除字符串将返回源字符串。
String str = StringUtils.remove("Test remove", "remove");
System.out.println(str);
//result will be "Test"
答案 8 :(得分:1)
您可以使用replace
修复字符串。以下将返回“(”之前的所有内容,并且还会删除所有前导和尾随空格。如果字符串以“(”开头,它将保持原样。
str = "manchester united (with nice players)"
matched = str.match(/.*(?=\()/)
str.replace(matched[0].strip) if matched
答案 9 :(得分:1)
// 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);
}
}
答案 10 :(得分:0)
如果您只需要删除“(”之后的所有内容,请尝试此操作。如果没有括号,则不执行任何操作。
StringUtils.substringBefore(str, "(");
如果括号后可能有内容,请尝试此操作。
String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")");
要删除结束符,请使用str.trim()
Apache StringUtils函数为null,empty和没有匹配安全性
答案 11 :(得分:0)
如果要从末尾删除特定的字符串,请使用 removeSuffix
(Documentation)
var text = "one(two"
text = text.removeSuffix("(two") // "one"
如果字符串中不存在后缀,则仅返回原始后缀
var text = "one(three"
text = text.removeSuffix("(two") // "one(three"
如果要在字符后删除,请使用
// 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, "")
您还可以签出相似的removePrefix
,removeRange
,removeSurrounding
和replaceAfterLast
完整列表在这里:(Documentation)