1

How to search for one word in a big message in Android?

I have a text like "The sun always shines above the clouds". I wanna search for a single word, like "sun", and change it to an image. How to do this? Is there any way?

Massimo
  • 3,436
  • 4
  • 40
  • 68
Shanaz K
  • 678
  • 2
  • 13
  • 28

4 Answers4

2
String word = "cat";
String text = "The cat is on the table";
Boolean found;

found = text.contains(word);
MehmetF
  • 61
  • 1
  • 14
  • thanks it's work but how to do that If I have many message I I have may be 10 message ? – Shanaz K Jan 16 '14 at 21:58
  • use hashmap dude. HashMap myhash = new HashMap myhash.put("0", yourstring1); myhash.put("1", yourstring2); for(i=0, myhash.size(), i++){ String abc = myhash.get(i); String word = "cat"; String text = "The cat is on the table"; Boolean found; found = text.contains(word); } – MehmetF Jan 16 '14 at 22:00
2

Regular Expressions in Java are the most flexible and powerful tools you can use to search and replace strings within other strings. Depending on where you display this data (eg. an HTML View perhaps?) you can replace the words with markup that can display an image or find the location in the string where you can break up elements to create TextViews vs ImageViews. On this latter case, another useful method within the String class might be the indexOf() or contains() methods.

Carlos N
  • 1,616
  • 14
  • 15
  • 1
    Regex are good for matching patterns. Since it's relatively expensive using it - better use one of the other two suggestions: `indexOf()` and `contains()` – Nir Alfasi Jan 16 '14 at 21:56
1

To find the position of a given word in a string use the method

public int indexOf (String string)

For replacing strings with other strings you can use

public String replaceAll (String regularExpression, String replacement)

It is not clear what you mean with "I wanna search for single word like (sun) and change to an image"

Massimo
  • 3,436
  • 4
  • 40
  • 68
  • If you have just to replace occurrencies of a string with another string you can use the replaceAll method: String result = original.replaceAll("sun", "moon") – Massimo Jan 16 '14 at 22:00
  • is there any way to do this? I want to change the word to an image – Shanaz K Jan 16 '14 at 22:03
  • No, there is not a simple/common way. I think you can achieve that result by using a WebView, writing your string in a HTML page (or something similar), inserting tags where you want to insert images. – Massimo Jan 16 '14 at 22:07
  • look I wanna create a chat app when user enter :P change to an icon how to do this ? – Shanaz K Jan 16 '14 at 22:16
  • This is the answer you are searching for http://stackoverflow.com/questions/3341702/displaying-emoticons-in-android/4302199#4302199 :D – Massimo Jan 16 '14 at 22:18
0

An easy way is to use the String.replace method:

String source="The (sun) is shining.";
String replaced=source.replace('(sun)', '<img href="a_sun.png">');

See: http://javarevisited.blogspot.se/2011/12/java-string-replace-example-tutorial.html

Mikael Lindqvist
  • 775
  • 5
  • 21