Wednesday, April 10, 2013

Removing the last comma if it exists

Recently i faced this issue of removing the last character in a String if it is a comma.
I wanted to do this with a regular expression although i could have very easily done this by writing some custom code.

This is how you'll do this using regular expression

Pattern pattern = Pattern.compile(",$",0);
String gg=pattern.split(comma)[0];

It is strange that
string.replace(",$", "");
did not work for me even though this uses regular expressions internally.

I could have also used

if (string.charAt(string.length()-1) == ',' )
        System.out.println(string.substring(0, string.length()-1));

No comments:

Idea for patent

Return machine Id  and use this for routing future requests . This will give cache locality. The ui/ mobile app should use this I'd f...