Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 888
srini Avatar asked Aug 15 '12 09:08

srini


Video Answer


1 Answers

I would use the ^ anchor together with replaceFirst():

niceString = yourString.replaceFirst("^,", "");
like image 52
Keppil Avatar answered Oct 18 '22 19:10

Keppil