-8

After googeling alot I still have no found the answer that I am looking for .. Hopefully you guys can point me into the right direction.

Currently I have a working app using Android Studio which allows me to replace a word from user input to another word, basically a if else function. However, this won't allow me replace a whole sentence.

What I am trying to achieve is:

I want to have a text field where a user can input a text.

For example, the user types: "Hello, I am John Doe and I like chocolate"

The user the submits his entry using a send button. I then want a function which replaces only a certain word(s) within his input.

For example, I want a function that replaces the word "chocolate" with "milk".

So that the user gets a return from the system, saying: "Hello, I am John Doe and I like milk". But I also want it to replace words in between words. For example, I also want the function the replace the name "John" with "Mary".

Which function can I use to achieve this scenario?

Thanks for reading.

Glenn

Glenn
  • 45
  • 5
  • I don't have a code to supply. It's a question about a function. I'm not asking you to "fix" my code. I'm asking you to push me into the right direction so I can fix it myself. – Glenn Dec 29 '16 at 13:34
  • 3
    [String.replace](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)) – jhamon Dec 29 '16 at 13:36
  • "basically a if else function" well you do have some code then. Post what you have. – weston Dec 29 '16 at 13:55
  • 2
    Possible duplicate of http://stackoverflow.com/questions/5216272/replace-string-with-another-in-java or any other string replacement question in java. – Edson Menegatti Dec 29 '16 at 14:01
  • Tokenize the sentence, then use switch to replace the words (if you are looking for any specific words). Else if you want to do this based on context and part of speech parsing, then you should look at some nlp library. – gvmani Dec 29 '16 at 14:14
  • The potential advantage of `String.split()` or other ways to divide into words is that you can limit replacement to whole words. Useful if you want to replace “and” with “or” but don’t want “sands“ replaced with “sors”. – Ole V.V. Dec 29 '16 at 14:21

1 Answers1

0

In Android, inside a Layout you insert an EditText, it's that text field you wanted where you can write text into it and also listen to text changes so you can do those manipulations you were talking about.

Create a TextWatcher and add it to the EditText using addTextChangedListener(TextWatcher variable)

new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

Add your text manipulation functionality inside these callbacks, hope that's a good direction for you

TIP: create your own class for a custom text field and extend from EditText, now every where you place that custom view you will have the text manipulation functionality, you could addTextChangedListener from within that class.

Ariel Yust
  • 585
  • 4
  • 11