简体   繁体   中英

delete dynamic character from string

I have a string containing numbers separated with , . I want to remove the , before the first character.

The input is ,1,2,3,4,5,6,7,8,9,10 , and this code does not work:

results.replaceFirst(",","");

Strings are immutable in Java. Calling a method on a string will not modify the string itself, but will instead return a new string .

In order to capture this new string, you need to assign the result of the operation back to a variable:

results = results.replaceFirst(",", "");

Try this

String str = ",1,2,3,4,5,6,7,8,9,10";
if(Objects.nonNull(str) && str.startsWith(",")){
  str = str.substring(1, str.length());
}

it will remove , at first position

you can also do like this ..

String str = ",1,2,3,4,5,6,7,8,9,10";
String stre = str.replaceFirst("^,", "");
Log.e("abd",stre);

Try this

String str = ",1,2,3,4,5,6,7,8,9,10";
str = str .startsWith(",") ? str .substring(1) : str ;
System.out.println("output"+str);  // 1,2,3,4,5,6,7,8,9,10

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM