Skip to content
Fix Code Error

Replace a character at a specific index in a string?

March 13, 2021 by Code Error
Posted By: Anonymous

I’m trying to replace a character at a specific index in a string.

What I’m doing is:

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

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

Solution

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 By: Anonymous

Related Articles

  • How can I resolve Web Component Testing error?
  • How to format a phone number in a textfield
  • Fastest way to iterate over all the chars in a String
  • Reduce vs Collect method on Parallel Streams vs…
  • Unable to run Robolectric and Espresso with a…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • When I'm testing a web app by JUnit and Mockito I…
  • Eclipse will not start and I haven't changed anything
  • ClassNotFoundException:…
  • How to convert number to words in java
  • How to add Typescript to a Nativescript-Vue project?
  • How do I allow any character that is not a letter of…
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • Neither BindingResult nor plain target object for…
  • java IO Exception: Stream Closed
  • Jetpack Compose and Hilt Conflict
  • Examples of GoF Design Patterns in Java's core libraries
  • SecurityException: Permission denied (missing…
  • Gradle error: Execution failed for task…
  • SQLException: No suitable Driver Found for…
  • Can't access Eclipse marketplace
  • Requested bean is currently in creation: Is there an…
  • Using enums in a spring entity
  • android studio 0.4.2: Gradle project sync failed error
  • Execution failed for task…
  • Best way to convert an ArrayList to a string
  • org.gradle.api.tasks.TaskExecutionException:…
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • How to parse JSON file with Spring
  • JPA Hibernate Persistence exception…
  • How to select and view database with specific value?
  • Check whether String2 is substring of string1 with…
  • Android- Error:Execution failed for task…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • Configure hibernate to connect to database via JNDI…
  • How do I install Java on Mac OSX allowing version switching?
  • Could not install Gradle distribution from…
  • Error inflating class android.support.v7.widget.Toolbar?
  • "Non-resolvable parent POM: Could not transfer…
  • Palindromic numbers in Java
  • How to convert java.util.Date to java.sql.Date?
  • Launching Spring application Address already in use
  • Correct way to use StringBuilder in SQL
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • ClassNotFoundException thrown
  • How are people implementing immutable data…
  • Eclipse fails to open .vue files in Web Page Editor
  • Vaadin Spring Boot - There was an exception while…
  • java.lang.ClassNotFoundException: HttpServletRequest
  • Java - Regex, nested recursion match
  • UnsatisfiedDependencyException: Error creating bean…
  • What does a "Cannot find symbol" or "Cannot resolve…
  • java.lang.RuntimeException: Unable to instantiate…
  • Plugin…
  • java.net.UnknownHostException: Unable to resolve…
  • Problem while using user insert for getter
  • Java ElasticSearch None of the configured nodes are…
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • Spring Boot - Cannot determine embedded database…
  • How to unescape a Java string literal in Java?
  • error LNK2005: ✘✘✘ already defined in…
  • Problems using Maven and SSL behind proxy
  • Is there a reason why it doesn't go to the 'else if'…
  • Powershell: A positional parameter cannot be found…
  • What is a stack trace, and how can I use it to debug…
  • Injection of autowired dependencies failed;
  • Gradle: Execution failed for task ':processDebugManifest'
  • Reference - What does this regex mean?
  • Address already in use: JVM_Bind
  • Spring schemaLocation fails when there is no…
  • What's the difference between Instant and LocalDateTime?
  • Eclipse - Run in Debug Mode fails
  • Multipart File Upload Using Spring Rest Template +…
  • NullpointerException error while working with…
  • How do I use 3DES encryption/decryption in Java?
  • What does "Could not find or load main class" mean?
  • Jackson cannot parse body of a POST request
  • Any quick way of updating a column based on timestamp
  • How to use an output parameter in Java?
  • Name [jdbc/mydb] is not bound in this Context
  • Failed to execute goal…
  • Basic Question - Setting object's traits in a method…
  • Changing the JFrame title
  • java.lang.ClassNotFoundException:…
  • Spring Security exclude url patterns in security…
  • Exception : AAPT2 error: check logs for details
  • What's causing my java.net.SocketException:…
  • Where is the mistake? I wanted to add the ability to…
  • Java.lang.NoClassDefFoundError:…
  • String is immutable. What exactly is the meaning?
  • Javax.net.ssl.SSLHandshakeException:…
  • Parcelable encountered IOException writing…
  • com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcept…
  • RxJava Main Thread Always Crash
  • Jetty server throws idle timeout for REST calls
  • What is the best way to get the first letter from a…
  • Create the perfect JPA entity
  • How to disable SSL certificate checking with Spring…
  • Error creating bean with name

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Regex for numbers only

Next Post:

How to deal with SettingWithCopyWarning in Pandas

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error