Removing spaces from a string

I am trying to remove all spaces from a string obtained from user input, but for some reason it does not work for me. Here is my code.

public void onClick(View src) { switch (src.getId()) { case R.id.buttonGo: String input = EditTextinput.getText().toString(); input = String.replace(" ", ""); url = ur + input + l; Intent myIntent = new Intent(start.this, Main.class); myIntent.putExtra("URL", url); start.this.startActivity(myIntent); break; } } 
+70
android string replace trim
Aug 03 '11 at 19:31
source share
5 answers
 String input = EditTextinput.getText().toString(); input = input.replace(" ", ""); 

Sometimes you only need to remove spaces at the beginning or end of a line (and not in the middle). If in this case you can use trim :

 input = input.trim(); 
+236
Aug 03 '11 at 19:32
source share

When I read the numbers from the contact book, then it doesn’t work. I used

 number=number.replaceAll("\\s+", ""); 

This worked, and for the url you can use

 url=url.replaceAll(" ", "%20"); 
+20
03 Feb '14 at 9:54
source share

I also had this problem. To solve the problem of spaces in the middle of the line, this line of code always works:

 String field = field.replaceAll("\\s+", ""); 
+3
Sep 19 '14 at 7:23
source share

Try the following:

 String urle = HOST + url + value; 

Then return the values ​​from:

 urle.replace(" ", "%20").trim(); 
+2
May 19 '15 at 6:46
source share

String res = "Application" res = res.trim ();

o / p: application

Note: empty space, empty space is cropped or deleted

0
Jan 21 '19 at 9:31
source share



All Articles