1

I am working with this piece of code:

start = str.indexOf('<');
finish = str.indexOf('>');
between = str.substring(start + 1, finish);
replacement = str.substring(start, finish + 1);
System.out.println(between + " " + replacement); //for debug purposes
forreplacement = JOptionPane.showInputDialog(null, "Enter a " + between);
System.out.println(forreplacement); //for debug purposes
counter = counter - 1;

where "str" is the String that is being worked with. How do I replace the substring "replacement" with the string "forreplacement" (what is entered into the popup box) within the string str?

2
  • Maybe I just don't understand. Please give an expected input-output. Commented Sep 6, 2013 at 19:31
  • 2
    Can you provide the inputs and ouputs strings? Commented Sep 6, 2013 at 19:33

2 Answers 2

2

Not sure if that is what you want but maybe take part before start, add forreplacemeent and after that add part after finish like

String result = str.substring(0, start)+forreplacement+str.substring(finish+1);

Or if you want to replace all occurrences of replacement with forreplacement you can also use

String result = str.replace(replacement, forreplacement)
Sign up to request clarification or add additional context in comments.

Comments

0

Your question is not clear. If you want to replace, you can do something like-

String str = "Hello World!";
String replacement = "World";
String stringToReplaceWith = "Earth";

str = str.replace(replacement, stringToReplaceWith);

System.out.println(str); // This prints Hello Earth!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.