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,040 points
30 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,520 points

Related Questions In Java

0 votes
1 answer

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

myString=" Hello World"; myString.split("\\s+"); This should produce the strings "Hello" and "World" and omit ...READ MORE

answered May 23, 2018 in Java by sharth
• 3,310 points
32 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,840 points
18 views
0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
57 views
0 votes
1 answer

How do I get the current date and time using Java?

To print current date and time using ...READ MORE

answered Apr 13, 2018 in Java by Akrati
• 3,150 points
45 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,040 points
206 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,520 points
44 views
0 votes
1 answer

String mapping without Case-Sensitivity

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

answered Feb 5 in Java by developer_1
• 3,280 points
24 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,040 points
499 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,520 points
68 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,840 points
1,857 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.