开发者

string ends with '\n' what is the most efficient way to manipulate?

it happens that the user click on enter where i dont want to include it as a part of my input the string can end with 3 times \n so just replacing one wont do the job my solution was ;

String values[] = string_Ends_With_Back_Slash_N.split("\n"); 
String String_without_Back_Slash_N =new String (values [0]);
//or just to point there without the new but i want later to 开发者_如何学运维dump the garbage.

or at least to dump values to the gc now ....

so two q :

  1. is there more efficent way?..
  2. who do i call the compiler (java on android ...)


Try String.trim()

This methods removes all characters from the ends of the String, which have an ascii code smaller than the space (interval, 32). This includes \n (10).


String String_without_Back_Slash_N = string_Ends_With_Back_Slash_N.trim()


String s = "test\n\n\n";
s.replaceAll("\n", "");


Since strings are immutable there is no need to create a new string. If you dump the "garbage" the garbage collector will recognize that the first element in the array is still referenced and not throw it away. The rest will be disposed of.


replace all \n (and maybe \r) with ""

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜