How do I replace character from string at specific indexes

0 votes

I need to replace character from string at specific index.

This is how I am trying to do it:

String myName = "domanokz";
myName.charAt(4) = 'x';

This gives an error. Is there any method to do this?

Apr 26, 2018 in Java by Daisy
• 8,140 points
23,834 views

2 answers to this question.

0 votes

String are immutable in Java. You can't change them.

You need to create a new string with the character replaced.

String myName = "domanokz";
String newName = myName.substring(0,4)+'x'+myName.substring(5);

Or you can use a StringBuilder:

StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');

System.out.println(myName);

answered Apr 26, 2018 by Rishabh
• 3,620 points
0 votes

You could turn the String into a char[], replace the letter by index, then convert the array back into a String.

String myName = "domanokz";
char[] myNameChars = myName.toCharArray();
myNameChars[4] = 'x';
myName = String.valueOf(myNameChars);

Hope this helps!

answered Aug 22, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
1 answer

How do I convert from int to String?

Normal ways would be Integer.toString(i) or String.valueOf(i). The concatenation will work, ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,770 points
700 views
0 votes
0 answers

How do I replace all occurrences of a string in JavaScript?

Given a string: s = "Test abc test ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
594 views
0 votes
2 answers

How can I split the String with any whitespace character as delimiter?

String string = "Today's weather"; String[] arrayOfString = ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
1,165 views
0 votes
2 answers

How can I do url string decoding in java

We can use URLDecoder: URLDecoder.decode( url, "UTF-8" ); READ MORE

answered Dec 7, 2018 in Java by Sushmita
• 6,920 points
831 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,131 views
0 votes
1 answer

What is the 'instanceof' operator used for in Java?

It's an operator that returns true if ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,620 points
1,112 views
0 votes
1 answer

String mapping without Case-Sensitivity

Yes, contains is case sensitive. You can ...READ MORE

answered Feb 5, 2019 in Java by developer_1
• 3,350 points
844 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,738 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,211 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
17,261 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP