mardi 28 avril 2020

String pattern detection

I want to turn a string into a html tag with a specified pattern like this:

Walking in the !boldstreet

I want to detect the !bold in the string and turn the following word into "<b>street</b>".

    String s = "Walking in the !boldstreet" 
    String result = null;
    String[] styles = {"!bold", "!italic", "!deleted", "!marked"};
    String[] tags = {"b", "i", "del", "mark"};
    List<String> indexer = new ArrayList<String>(Arrays.asList(tags));
    int index = 0;
    for (String x: styles) {
        if (s.startsWith(x)) {
            result = style(s, indexer.get(index));
            break;
        }
        index++;
    }
    if (result == null) result = s; //there was no formatting


    return result;

      //style method
      public String style(String text, String tag) {
    return "<" + tag + ">" + text + "</" + tag + ">";
}

This works but when i pass something like this to it: "Walking !deletedin the !boldstreet"

only it will turn !boldstreet into html tags. How can i make it turn everything like those into html tags?

Aucun commentaire:

Enregistrer un commentaire