-1

in my app i have an EditText, that its text is "Hello my friend, Hello". how can i replace second hello with goodbye? i want to change its text to "Hello my friend, goodbye". i used replace() statement but replaces all hello words with goodbye. can i get letter index and use for replacing? for example i say to program that replace letters from 18 to 22 with goodbye. this is my code:

String text = edtText.getText().toString().replace("Hello", "goodbye");
edtText.setText(text);
4
  • Check this answer: stackoverflow.com/questions/1660034/replace-last-part-of-string Commented Jan 15, 2015 at 10:41
  • you check the substring using start and end point Commented Jan 15, 2015 at 10:43
  • 1
    This has nothing to do with EditText, it is a simply a matter of replacing a word in a String. And that has surely been answered before. Commented Jan 15, 2015 at 10:46
  • Try this answer by BalusC stackoverflow.com/a/2282982/2998224 I tested it with your string and it worked well. Commented Jan 15, 2015 at 10:57

4 Answers 4

1

Try this:

String actualString = "Hello my friend, Hello";
String finalString = actualString.substring(0, actualString.lastIndexOf("Hello")) +
                     "goodbye" +
                     actualString.substring(actualString.lastIndexOf("Hello") + "Hello".length(), actualString.length());
Sign up to request clarification or add additional context in comments.

Comments

0

This should work:

public static String replace(String phrase, String before, String after, int index) {
    String[] splittedPhrase = phrase.split(" ");
    String output = "";
    for (int i = 0, j = 0; i < splittedPhrase.length; i++) {
        if (i != 0)
            output += " ";
        if (splittedPhrase[i].equals(before) && index == j++)
            output += after;
        else
            output += splittedPhrase[i];
    }
    return output;
}

Or, in a shorter way:

public static String replace(String phrase, String before, String after, int index) {
    int i = 0;
    String output = "";
    for (String s : phrase.split(" "))
        output += ((s.equals(before) && index == i++) ? after : s) + " ";
    return output.substring(0, output.length() - 1);
}

Then, simply:

public static void main(String args[]) {
    System.out.println(replace("hello world, hello", "hello", "goodbye", 0));
    System.out.println(replace("hello world, hello", "hello", "goodbye", 1));
    System.out.println(replace("hello world, hello", "hello", "goodbye", 2));
}

Printed out:

"goodbye world, hello"
"hello world, goodbye"
"hello world, hello"

Comments

0

Your solution.

 et = (EditText) findViewById(R.id.editText);
 String text = "Hello my friend , Hello";
 et.setText(text);
 String[] txt = text.split("Hello");
 for (int x = 0 ; x < txt.length ; x++)
     System.out.println(txt[x]); //for cast
 text = "goodbye" + txt[0] + txt[1] + "goodbye";
 et.setText(text);

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. Font

Comments

0

Try this:

string hello = "hello";
int start =0;
int end =0;
String text = edtText.getText().toString();
String replacement = "replacement";
start=text.indexOf(hello, str.indexOf(hello) + 1);
end=start+hello.lenght();

StringBuilder s = new StringBuilder();
 s.append(text.substring(0, start)));
 s.append(replacement);
 s.append(text.substring(end);
edtText.setText(s.toString());

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.