Question: Replacing characters in Java without replace function. So I am asking for a string to be inputted, then I am asking what the user wants

Replacing characters in Java without replace function.

So I am asking for a string to be inputted, then I am asking what the user wants to do to the string, in this case, I am trying to replace the first occurrence of a character. I can ask for what wants to be replaced and what to replace it with. I also know how to do the replacement somewhat, but say if the string has 2 of the same characters, I get a new String for both. I cannot use the replace function, index function. Only length, concat, substring, length, and equals

I only need the first one.

*Eclipse Java Photon*

else if (UserCommand.equalsIgnoreCase("replace first")) { System.out.println("Enter the character to replace"); Replace =

Output

input.next(); System.out.println("Enter the new character"); Replacement = input.next(); for (int i =

else if (UserCommand.equalsIgnoreCase("replace first")) { System.out.println("Enter the character to replace"); Replace = input.next(); System.out.println("Enter the new character"); Replacement = input.next(); for (int i = 0; i < String.length();i++) { if (String.charAt(i)==Replace.charAt(0)) { StringEdit } = String.substring(0,i) + Replacement + String.substring(i + Replace.length()); System.out.print(StringEdit); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!