我想从一个字符中删除字符串的一部分,即:
源字符串:
目标字符串:
源字符串:
manchester united (with nice players)
目标字符串:
manchester united
// 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