views:

86

answers:

5

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 ...)
+5  A: 

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).

Bozho
+4  A: 
String String_without_Back_Slash_N = string_Ends_With_Back_Slash_N.trim()
Sjoerd
'/n' is no " "(white space ) therefore your approach is irrelevant ...
yoav.str
@yoav.str try it, it works ;)
Bozho
i have tried ... :)it didnt delete the /n ...in java on eclipse... in c# i think it does work i don't have a compiler but this is a nuce thing if someone can check
yoav.str
@yoav.str I checked and it worked. It is not `/n` - it is `\n`
Bozho
A: 

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

Fredrik Leijon
or use trim() :)
Fredrik Leijon
yes but the reason i ask for guidelines is that i want to improve efficiency therefore your answer doesnt help me.
yoav.str
+1  A: 
String s = "test\n\n\n";
s.replaceAll("\n", "");
Maciej Dragan
the return type is the one i look forString temp = s.replaceAll("\n", "");but still my question is there more efficient way to do this ?
yoav.str
A: 

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.

Peter Tillemans