简体   繁体   English

从字符串中删除第n个字符

[英]delete n-th character from string

There is a String "Saya" I want to delete 4th character so it will be "Say" 有一个字符串“ Saya”,我想删除第四个字符,因此它将是“ 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". 但是结果是“ Sy”。 It delete all character that same with 4th 删除所有与第四个相同的字符

Does anyone can help me? 有人可以帮助我吗?

You want to use substring() . 您想使用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 确保检查原始字符串的长度,否则可能会收到IndexOutOfBoundsException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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