views:

118

answers:

2

Hi,

I am creating a request string which hast bo be formatted the following way:

http://staging.myproject.com?tags=tag1,tag2,tag3

There is an EditText dialog on the form where the user writes the tags. The user however can write the tags in the EditText whatever way he wants, for example using commas: tag1, tag2, tag3 or tag1,tag2,tag3 (without spaces) or tag1 tag2 tag3 or for example someone could make multiple spaces between two tags: tag1 tag, tag3...

I have to make a logic to reshape the content of the EditText the way it matches format

"tag1,tag2,tag3"

(remove spaces, add commas if necessary...).

Does anyone know what is the easiest way to do it in Java?

Thank you.

+2  A: 

Try this:

editText = editText.replaceAll("[, ]+", ",");
SLaks
A: 

The best generic way to do this IMHO is Regular Expressions (you have the standard Java RegExp avalaible in the Android SDK). Google "RegExp" if you don't know about it.

Fabien