husnul
husnul

Reputation: 130

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?

Upvotes: 0

Views: 8611

Answers (1)

Jason Sperske
Jason Sperske

Reputation: 30436

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

Upvotes: 11

Related Questions