title: Replace the characters with Image in string and then set to Textview
body: <pre><code>This is :) and want to :) replace with :D new image.
  </code></pre>
  
  <p>I have this type of string that is i have got from EditTextbox.NOw i want to replace all ":)" with image1 and ":D" with image2.I want to do like string.replaceall(":)",image1) and string.replaceall(":D",image2).So can anybody suggest me how to do this with small code and also better performance.I have write the code and it is working also fine but it takes much time.</p>
  
  <pre><code>textview.setText(getSmiledText(ctx, stringvalue));
  private static final HashMap&lt;String, Integer&gt; emoticons = new HashMap&lt;String, Integer&gt;();
      static {
          emoticons.put(":)", R.drawable.j1);
          emoticons.put(":D", R.drawable.j2);}
  
  public static Spannable getSmiledText(Context context, String s) {
          int index;
          SpannableStringBuilder builder = new SpannableStringBuilder();
          builder.append(s);
  
          for (index = 0; index &lt; builder.length(); index++) {
              for (Entry&lt;String, Integer&gt; entry : emoticons.entrySet()) {
                  int length = entry.getKey().length();
                  if (index + length &gt; builder.length())
                      continue;
                  if (builder.subSequence(index, index + length).toString()
                          .equals(entry.getKey())) {
                      builder.setSpan(new ImageSpan(context, entry.getValue()),
                              index, index + length,
                              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      index += length - 1;
                      break;
                  }
              }
          }
          return builder;
      }
  </code></pre>