简体   繁体   中英

delete n-th character from string

There is a String "Saya" I want to delete 4th character so it will be "Say"

I already do this

String word = "Saya";
char c = word.charAt(3);
String delete = Character.toString(c);
String newWord = word.replace(delete,"");
System.out.println(newWord);

But the result is "Sy". It delete all character that same with 4th

Does anyone can help me?

You want to use substring() . Like so:

i = 3;
String newWord = word.substring(0,i)+word.substring(i+1);

Make sure you check the length of your original string otherwise you may get an IndexOutOfBoundsException

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