Treat a word with dots

Asked

Viewed 102 times

4

I have a method that takes a text as a parameter and makes a .split in the text separating the words by " ". For each word in the text I make a check to see if the word is equal to the item in my list of Enum, if the word is equal to Enum the word takes a format for css. How do I separate the words by " " some words may come precedent or proceeding with a .(point). I would like to treat only the word by ignoring the point and after treating the word, return the point to its place, how can I do ? I thought of some situations but without success. 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 HP is below 1/3, FIRE type moves win a bonus of 50% Rass.

There are 3 words to be formatted: Electric, fire and Grass. However Electric has a point before and Grass has the point after. One should ignore the points and format only 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, e.g.:

.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;
}

At @Sorack’s suggestion, follow the print of how my datatable looked: inserir a descrição da imagem aqui

The text generated in the first column was this:

<span class="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
  • 1

    You can add an example of these separate words with space and containing the dot?

1 answer

5


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

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

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 make the substitution, without having to go through the 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 the replace with $ + the position of that group;

  • The | is an OR clause in the regular expression, meaning you will find Electric-type and Electric as well;

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

Electric-type: When HP is below 1/3, FIRE type moves earn 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>.

  • I adapted in my code, and I took a print for you, I will edit in my question the result that came out in datatable.

  • @Douglas but so, surely this is the result of some kind of hybrid that you had not specified, no? What text generated that problem in the first column?

  • Sorack, added in question.

  • @Douglas not the generated text. The text before going through the changes. It can be here in the same comment, but I already tell you that it seems that was executed the function 2x for that text

  • Sorack you shone man, I was really calling the method 2 times. Thanks so much for the help. :)

  • Some words are not being captured :(

  • I removed the . and it worked well.

  • @Douglas What kind of words?

  • The ones in the Enum, I realized that the case occurred when they were proceeding to the point, they were not entering the regex, I removed the point of the regex and now it works perfectly.

  • @Douglas but when removing (.) you have to remove the group afterwards as well. You did this?

  • 1

    Yes, I did, thank you.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.