In Java, remove the first char of the string if it is , (comma)

srini picture srini · Aug 15, 2012 · Viewed 29k times · Source

In Java, I have a String variable.

Sometimes the first character of the string is a comma ,

I want to remove the first char only if it is a comma.

What is the best approach to do this?

Answer

Jon Skeet picture Jon Skeet · Aug 15, 2012

Something like:

text = text.startsWith(",") ? text.substring(1) : text;

is pretty simple...