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*
Output
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started