0

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 ...)
ant
  • 22,634
  • 36
  • 132
  • 182
yoav.str
  • 1,547
  • 6
  • 33
  • 73
  • can you use StringUtils in Android? There are some handy functions there for dealing with Strings. http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html – pakore Jun 24 '10 at 10:05
  • Possible duplicate of http://stackoverflow.com/questions/3100585/remove-end-of-line-characters-from-end-of-java-string – user85509 Jun 24 '10 at 10:06
  • I'd also encourage you to get used to the language's conventions - although it has nothing to do with the question per se, your variables should conventionally in lower camel case, e.g. `String stringWithoutBackSlashN`. I actually found it noticably harder to read your code because you're violating the naming conventions. – Andrzej Doyle Jun 24 '10 at 10:07
  • andrzej you right i started writing camel case but it appeared here ugley therefore i changed it to the one who's showen – yoav.str Jun 24 '10 at 10:18

5 Answers5

5

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
  • 588,226
  • 146
  • 1,060
  • 1,140
4
String String_without_Back_Slash_N = string_Ends_With_Back_Slash_N.trim()
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • 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 Jun 24 '10 at 10:24
  • @yoav.str I checked and it worked. It is not `/n` - it is `\n` – Bozho Jun 24 '10 at 11:55
1
String s = "test\n\n\n";
s.replaceAll("\n", "");
Maciej Dragan
  • 485
  • 3
  • 5
  • the return type is the one i look for String temp = s.replaceAll("\n", ""); but still my question is there more efficient way to do this ? – yoav.str Jun 24 '10 at 10:21
0

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

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
Fredrik Leijon
  • 2,792
  • 18
  • 20
  • yes but the reason i ask for guidelines is that i want to improve efficiency therefore your answer doesnt help me. – yoav.str Jun 24 '10 at 10:15
0

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
  • 34,983
  • 11
  • 83
  • 114