user6363583 user6363583 - 1 year ago 49
Java Question

How to get keywords from a specific index in java

Suppose, I have a string

String advise = "eat healthy food";

Now my question is that :- In the string I only know the keyword healthy. I don't know what has before the word nor what has after the word. I just only know the middle word. So how can I get the before (eat) and after (food) keyword of "healthy"?

Note: Here the middle word's size is always specfic but the other two word's size is always different. Here 'eat' and 'food' have been used for an example only. These two words may be anything anytime

Also for note: I need to get these two words into two different string. Not in a same String.

Answer Source

Just split the string:


The first value in the array will be "eat", the second will be "food".