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?
Something like:
text = text.startsWith(",") ? text.substring(1) : text;
is pretty simple...
我会将^
锚与replaceFirst()
一起使用:
niceString = yourString.replaceFirst("^,", "");
如果您的类路径中有 commons-lang,可以查看StringUtils.removeStart(String str, String remove)
Try this
public String methodNoCharacter(String input, String character){
if(input!= null && input.trim().length() > 0)//exist
if(input.startsWith(character))//if start with '_'
return methodNoCharacter(input.substring(1));//recursive for sure!
return input;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.