开发者

string manipulation in Java: replace spaces and commas with one comma

开发者 https://www.devze.com 2022-12-15 03:18 出处:网络
I am creating a request string which hast to be formatted the following way: http://staging.myproject.com?tags=tag1,tag2,tag3

I am creating a request string which hast to 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?


Try this:

editText = editText.replaceAll("[, ]+", ",");
0

精彩评论

暂无评论...
验证码 换一张
取 消