-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);
hosseinAmini
  • 2,184
  • 2
  • 20
  • 46

4 Answers4

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());
Hemanth
  • 2,717
  • 2
  • 21
  • 29
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"
Giulio Biagini
  • 935
  • 5
  • 8
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

CodeNoob
  • 345
  • 4
  • 17
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());
QArea
  • 4,955
  • 1
  • 12
  • 22