How do you get part of a String?

Asked

Viewed 318 times

1

I just need to get the [nItem:2] of Phrase:

Nota fiscal - 502: Status do retorno da transmissão: 778 - Informado NCM inexistente [nItem:2]

Would anyone have any idea?

Remembering that only interests the excerpt [nItem:XXX]

  • Is this data constant? Or can it be other values than 2?

  • May contain other values

  • Is there any part of that stretch that doesn’t change?

  • The best thing is to give more examples of situations where you get the values, to be able to understand better what the pattern is. Very often one gives only one particular example, when in other situations one already needs to act differently

  • The text is static what can change is the number of the invoice, in case 502, the return of the transmission, in case 778 and the nItem, in case 2, which can have more than 1 character.

  • 1

    @Leandrosantos for you only interests the stretch [nItem:XXX] correct?

  • That is correct...

Show 2 more comments

3 answers

6


We already have some answers that meet the scenario, however, I would like to share my solution using regular expressions.

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

public class q317584 {

    public static void main(String[] args) {

        String linha = "Nota fiscal - 502: Status do retorno da transmissão: 778 - Informado NCM inexistente [nItem:2]";
        Pattern pattern = Pattern.compile("\\[nItem:(\\d*)]");
        Matcher matcher = pattern.matcher(linha);
        if(matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}

One of the advantages of this approach is assertiveness.

Regardless of what comes before, then, more spaces, less spaces, the Pattern will always catch the next number after the tag [nItem:

I also believe that it makes code cleaner and more intelligible, without using .split, .substring, that depend on magic numbers to make the solution viable.

2

Taking into account your need from the clarifications in the comments of the question, this should suffice:

String linha = "Nota fiscal - 502: Status do retorno da transmissão: 778 - Informado NCM inexistente [nItem:2]";
System.out.println(linha.substring(linha.lastIndexOf(":")+1, linha.length()-1)); //imprimirá 2
  • Thanks for the reply friend, however if the number is 10, he will only take the correct 0 ? Then I will have problems

  • Guy had not tested, solved my problem, sorry ignorance thank you so much!

-1

Break the string with .split(" ") using space as a separator, and then grab the last item from the :P list

  • Friend thanks for the answer, I tried so much so I got a mistake. String test[] = o.split("["); System.out.println("ITEM "+test[0]); //prints item String errorTreated = test[0]; errorTreated = errorTreated.replace("[nItem", "); erroTreated = errorTreated.replace("]", "); System.out.println("ITEM NUMBER: "+errorTreated);

Browser other questions tagged

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