[英]Java String TRIM function not working
I am receiving a string from server trailing one or two lines of spaces like below given string. 我从服务器接收到一个字符串,该字符串尾随一两行空格,如给定字符串以下。
String str = "abc*******
********";
Consider * as spaces after my string 将*视为我的字符串后的空格
i have tried a few methods like 我尝试了几种方法
str = str.trim();
str = str.replace(String.valueOf((char) 160), " ").trim();
str = str.replaceAll("\u00A0", "");
but none is working. 但没有一个工作。 Why i am not able to remove the space?
为什么我无法删除空间?
You should try like this: 您应该这样尝试:
str = str.replaceAll("\n", "").trim();
You can observe there is a new line in that string . 您可以观察到该字符串中有一个新行。 first replace new line "\\n" with space("") and than trim
首先用空格(“”)替换新行“ \\ n”,然后修剪
You should do: 你应该做:
str = str.replaceAll("\n", "");
In my case use to work the function trim() 就我而言,用于工作函数trim()
试试这个: str = str.replaceAll("[.]*[\\\\s\\t]+$", "");
I have tried your 3 methods, and them all work. 我已经尝试了您的3种方法,它们都可以工作。 I think your question describing not correctly or complete, in fact, a String in java would not like
我认为您的问题描述不正确或不完整,实际上,java中的String不会
String str = "abc*******
********";
They must like 他们一定喜欢
String str = "abc*******"
+ "********";
So I think you should describe your question better to get help. 因此,我认为您应该更好地描述您的问题以获得帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.