我从Db
获取此字符串str = "External - Internal ";
我想从字符串中删除最后一个空格。我已经尝试过string.trim()并将其分配给另一个字符串 请建议,因为这不起作用。以下是我的参考代码。
public static void main(String args[]){
String str = "External - Internal ";
String temp = str.trim();
System.out.println("1"+temp);
temp=str.replaceAll(" ", "");
System.out.println("2"+temp);
temp=str.replace("\\r", "");
System.out.println("3"+temp);
}
此致 ABHI
答案 0 :(得分:0)
您只需通过string.replaceAll
或string.replaceFirst
功能即可完成此操作。
string.replaceAll("\\s(?=\\S*$)", "");
如果你的意思是最后的空格,那就使用下面的正则表达式。
string.replaceAll("\\s$", "");
如果您想处理一个或多个空格,请使用\\s+
代替\\s
。
答案 1 :(得分:0)
您可以在Strip Leading and Trailing Spaces From Java String
找到答案看看前两个答案。尝试正确修剪为myString.replaceAll(" \ s + $","");