0

this code used for check if word in string in hashMap key

String[] arrs = message.split("(?<! ) |(?<= {2})");

for(int j = 0 ; j < arrs.length; j++){

    if(AppConfig.hashMap.containsKey(arrs[j])){
        int s = AppConfig.hashMap.get(arrs[j]);
    } else 
        text.append(" "+arrs[j]);
}

and the hashMap its

public static Map<String, Integer> hashMap = new HashMap<String, Integer>()
{{
    put(":)", R.drawable.emoji_1f60a_64);
    put(":D", R.drawable.emoji_1f601_64);

    put(":'(", R.drawable.emoji_1f622_64);
    put(":P", R.drawable.emoji_1f61c_64);

    put(";)", R.drawable.emoji_1f609_64);
    put(":O", R.drawable.emoji_1f632_64);

    put("-_-", R.drawable.emoji_1f620_64);
    put(":*", R.drawable.emoji_1f618_64);
    put("<3", R.drawable.emoji_2764_64);
    put("^_^", R.drawable.emoji_2764_64);
}};

now its can replace :) with drawable emogi but the problem when i use another smile symbole

when i loop on string and compare every word if found in hashMap

 if(AppConfig.hashMap.containsKey(arrs[j])) //found smile replace with emogi

its check if there are :) or :D in string but problem when there are smiles symbols like those

 "","","","","","","",""

so the hashMap will be

public static Map<String, Integer> hashMap = new HashMap<String, Integer>()
{{
    put("", R.drawable.emoji_1f60a_64);
    put("", R.drawable.emoji_1f601_64);

    put("", R.drawable.emoji_1f622_64);
    put("", R.drawable.emoji_1f61c_64);

    put("", R.drawable.emoji_1f609_64);
    put("", R.drawable.emoji_1f632_64);

    put("", R.drawable.emoji_1f620_64);
    put("", R.drawable.emoji_1f618_64);
}};

here i have this string

hi how are you ?

now when check if there are key in hashMap equal or ... by

if(AppConfig.hashMap.containsKey(arrs[j]))

its fail and say no key with this string

Poger
  • 1,897
  • 18
  • 16
medo
  • 479
  • 3
  • 9
  • 24

1 Answers1

0

You could create a dynamic pattern from the values in the hashmap (and reuse it because it's expensive to create a pattern it for every execution).

Take a look at https://stackoverflow.com/a/1326962/6709113

For example:

package aa;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class Test {
      public static String replaceMultipleStrings(String message) {
        Map<String,String> tokens = new HashMap<String,String>();
        tokens.put("Garfield", "--Garfield--");
        tokens.put("", "----");
        tokens.put("", "----");
        tokens.put("", "----");

        // Create pattern of the format "(cat|beverage)"
        String patternString = "(" + StringUtils.join(tokens.keySet(), "|") + ")";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(message);

        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
        }
        matcher.appendTail(sb);

        String result = sb.toString();

        System.out.println(result);

        return result;
      }


      public static void main(String[] args)
      {
        replaceMultipleStrings("hi how are you, my friend Garfield   ?");
      }

}

The result is: "hi how are you, my friend --Garfield-- ---- ---- ----?"

If performance is required, the HashMap and Pattern creation should be done only once.

Community
  • 1
  • 1