I'm trying to remove all the spaces from a string derived from user input, but for some reason it isn't working 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; } }
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.
Remove spaces from std::string in C++ To remove this we will use the remove() function. With this remove() function it takes the beginning and end of the iterator, then takes the third argument that will be deleted from that iterator object.
The strip() method is the most commonly accepted method to remove whitespaces in Python. It is a Python built-in function that trims a string by removing all leading and trailing whitespaces.
String input = EditTextinput.getText().toString(); input = input.replace(" ", "");
Sometimes you would want to remove only the spaces at the beginning or end of the String (not the ones in the middle). If that's the case you can use trim
:
input = input.trim();
When I am reading numbers from contact book, then it doesn't worked I used
number=number.replaceAll("\\s+", "");
It worked and for url you may use
url=url.replaceAll(" ", "%20");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With