Treat a word with period

4

I have a method that takes text as a parameter and makes a .split in the text separating the words with " " . For each word in the text I do a check checking if the word is equal to the item in my list of Enum , if the word is equal to Enum the word is formatted for css. Since I separate words by " " , some words may come precedent or come with a . (period). I would like to treat only the word by ignoring the point and after treating the word, to return the point to its place, how can I do? I thought of some situations but to no avail. This is my method:

public static String checkTipoPokemon(String texto) {
        List<TipoPokemon> lista = Arrays.asList(TipoPokemon.values());
        String palavras[] = texto.split(" ");
        String tipoIngles = null;

        for (int i = 0; i < lista.size(); i++) {
            String tipo = lista.get(i).name().toUpperCase();
            tipoIngles = tipo + "-type";
            for (String palavra : palavras) {
                if (palavra.toUpperCase().equals(tipo) || palavra.toUpperCase().equals(tipoIngles.toUpperCase())) {
                    texto = texto.replace(palavra,
                            "<span id=" + "tipo-" + tipo.toLowerCase() + ">" + palavra + "</span>");
                }
            }
        }
        return texto;
    }

Ex: Note the following text:

  

electic. When the HP is below 1/3, FIRE moves win   a bonus of 50% grass.

There are 3 words to format: electric , fire and grass . However electric has a point before it and grass has the point afterwards. You should ignore the dots and just format the word thus getting at the end:

.<span id="tipo-electric">electric</span>
<span id="tipo-grass">grass</span>.

Depending on the word, it will have a different formatting, but the words will have similar formatting, eg:

.tipo-pokemon span {
    text-transform: uppercase;
    font-weight: bold;
    font-style: italic;
}

.tipo-pokemon span#fire {
    color: #EE8130;
}

.tipo-pokemon span#water {
    color: #6890F0;
}

.tipo-pokemon span#ice {
    color: #98D8D8;
}

.tipo-pokemon span#electric {
    color: #F8D030;
}

.tipo-pokemon span#grass {
    color: #78C850;
}

.tipo-pokemon span#dark {
    color: #705848;
}

.tipo-pokemon span#bug {
    color: #A8B820;
}

.tipo-pokemon span#ghost {
    color: #705898;
}

.tipo-pokemon span#rock {
    color: #B8A038;
}

.tipo-pokemon span#fairy {
    color: #EE99AC;
}

.tipo-pokemon span#ground {
    color: #E2BF65;
}

.tipo-pokemon span#dragon {
    color: #7038F8;
}

.tipo-pokemon span#psychic {
    color: #F85888;
}

.tipo-pokemon span#poison {
    color: #A040A0;
}

.tipo-pokemon span#normal {
    color: #A8A878;
}

.tipo-pokemon span#fighting {
    color: #C03028;
}

.tipo-pokemon span#steel {
    color: #B8B8D0;
}

.tipo-pokemon span#flying {
    color: #A890F0;
}

Following @Sorack's suggestion, follow the print of how my dataTable:

Thetextgeneratedinthefirstcolumnwasthis:

<spanclass="tipo-pokemon <span class="tipo-pokemon grass">grass</span>"><span class="tipo-pokemon grass">grass</span></span> <span class="tipo-pokemon <span class="tipo-pokemon electric">electric</span>"><span class="tipo-pokemon electric">electric</span></span>. Quando o HP está abaixo de 1/3, os moves do tipo <span class="tipo-pokemon <span class="tipo-pokemon fire">fire</span>"><span class="tipo-pokemon fire">FIRE</span></span> ganham um bônus de 50% grass
    
asked by anonymous 25.11.2016 / 00:40

1 answer

5

I took the liberty of making some corrections in points that were not pointed out in the question:

  • You are using id in element span , which is incorrect if you have one more element to apply estilo , so I changed to class and corrected CSS (I added HTML with the result + CSS at the end of the response to check the corrections in generation and CSS );
  • I changed the word electric that was in the wrong spelling in the example sentence.

I created the following example class:

public class Pokemon {

  public enum Tipo {
    FIRE, WATER, ICE, ELECTRIC, GRASS, DARK, BUG, GHOST, ROCK, FAIRY, GROUND, DRAGON, PSYCHIC, POISON, NORMAL, FIGHTING, STEEL, FLYING;
  }

  public String substituir(String texto) {
    for (Tipo tipo : Tipo.values()) {
      String classe = tipo.name().toLowerCase();

      // (?i) é uma expressão regular para ignorar o case das palavras. Por exemplo: Irá encontrar Electric, ELECTRIC e electric
      // Os parênteses indicam um grupo, que será utilizado na segunda parte do replace com $ + a posição desse grupo
      // o | é uma cláusula OU na expressão regular, ou seja, irá procurar electric-type e electric também
      texto = texto.replaceAll("(?i)(" + classe + "-type|" + classe + ")", "<span class=\"tipo-pokemon " + classe + "\">$1</span>");
    }

    return texto;
  }

  public static void main(String[] args) {
    Pokemon pokemon = new Pokemon();

    System.out.println(pokemon.substituir("Electric-type: Quando o HP está abaixo de 1/3, os moves do tipo FIRE ganham um bônus de 50% grass."));
  }
}

Where I use regular expression to do the substitution, without having to go through array of words.

In regular expression :

  • (?i) is a regular expression to ignore the case of words. For example: You will find Electric, ELECTRIC and electric;

  • The parentheses indicate a group, which will be used in the second part of replace with $ + the position of that group;

  • The | is an OR clause in the regular expression, ie it will look for electric-type and electric as well;

The executable code working with the entry below can be checked in IDEONE and HTML +% generated% can be checked below.

  

Electric-type: When HP is below 1/3, FIRE moves gain a 50% grass bonus.

span.tipo-pokemon {
  text-transform: uppercase;
  font-weight: bold;
  font-style: italic;
}

span.tipo-pokemon.fire {
  color: #EE8130;
}

span.tipo-pokemon#water {
  color: #6890F0;
}

span.tipo-pokemon.ice {
    color: #98D8D8;
}

span.tipo-pokemon.electric {
    color: #F8D030;
}

span.tipo-pokemon.grass {
    color: #78C850;
}

span.tipo-pokemon.dark {
    color: #705848;
}

span.tipo-pokemon.bug {
    color: #A8B820;
}

span.tipo-pokemon.ghost {
    color: #705898;
}

span.tipo-pokemon.rock {
    color: #B8A038;
}

span.tipo-pokemon.fairy {
    color: #EE99AC;
}

span.tipo-pokemon.ground {
    color: #E2BF65;
}

span.tipo-pokemon.dragon {
    color: #7038F8;
}

span.tipo-pokemon.psychic {
    color: #F85888;
}

span.tipo-pokemon.poison {
    color: #A040A0;
}

span.tipo-pokemon.normal {
    color: #A8A878;
}

span.tipo-pokemon.fighting {
    color: #C03028;
}

span.tipo-pokemon.steel {
    color: #B8B8D0;
}

span.tipo-pokemon.flying {
    color: #A890F0;
}
<span class="tipo-pokemon electric">Electric-type</span>: Quando o HP está abaixo de 1/3, os moves do tipo <span class="tipo-pokemon fire">FIRE</span> ganham um bônus de 50% <span class="tipo-pokemon grass">grass</span>.
    
25.11.2016 / 02:05