B. Money
B. Money

Reputation: 931

Why Aren't Spaces and Characters Being Removed From String?

I'm a noob to android and I am having issues altering a string. I have a listview that is being populated from an arraylist. In my OnItemClick method, I want to take the value of the selection make it lowercase, remove the spaces, and remove the apostrophes. However, I have only been able to make the string lowercase and the space and apostrophes remain. For instance, "Bear's Garlic" becomes "bear's garlic" and not "bearsgarilc". Any help is greatly appreciated.

MY CODE

String herb_pic = herb_ListView.getItemAtPosition(position).toString().toLowerCase() + "_picture";
                        herb_pic.replaceAll("\\s+", ""); //Not removing whitespaces
                        herb_pic.replace(" ", ""); //Not removing space
                        herb_pic.replace("'", ""); //not removing apostrophe
                        herb_pic.replace(".", ""); //Not removing 
                        Log.e("herb_pic result", herb_pic); 

Upvotes: 0

Views: 238

Answers (2)

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

Because the replacefunction returns a String where the characters have been replaced.

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

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

Because in java Strings are immutable:

String herb_pic = herb_ListView.getItemAtPosition(position).toString().toLowerCase() + "_picture";
herb_pic = herb_pic.replaceAll("\\s+", ""); //Not removing whitespaces
herb_pic = herb_pic.replace(" ", ""); //Not removing space
herb_pic = herb_pic.replace("'", ""); //not removing apostrophe
herb_pic = herb_pic.replace(".", ""); //Not removing 
Log.e("herb_pic result", herb_pic); 

Upvotes: 3

Related Questions