How to find out if each character after the variable i of a for, is a number, and if it is to add a comma at the end

1

I have the following:

    for (int i = 0; i < result.length(); i++) 
    {
       if(result.substring(i).matches("[0-9]+") && result.substring(i+1).matches("[0-9]+"))
       {
           result += ",";
       }
    }

What I want to do is to look in a string if there is a character (sign) and separate it from the digits, what the above code does is search the string for a number and if the next character is also a number from 0 to 9 put a comma to separate it from the signs, but the error is in the fact that it only serves for 2 digits and not 3 or 4 or more that's what I want to finish doing thanks. If you do not explain well or understood something of what I wrote please write me in the comments I am new to this platform also thank you very much !!

    
asked by Kenji Miyashiro 28.10.2017 в 05:23
source

2 answers

1

Why does not your code go? because substring is badly used. substring returns from the string, to the end of the string. with which, unless they are all numbers, will not return anything.

In this case, it is convenient to go to a character and check if the previous one was a number or not.

String cadena = "123423-2132321-21-2223-21";
String resul = "";
Boolean esnumero = true;
for (int i = 0; i < cadena.length()-1; i++) 
{
   if(cadena.substring(i,i+1).matches("[0-9]+"))
   {
       esnumero = true;
       resul = resul + cadena.substring(i,i+1);
   }
   else if(esnumero)
   {
        esnumero = false;
        resul = resul + "," + cadena.substring(i,i+1);
   }
}

The code that happens to you, iterates the chain one character at a time, adding it to a result string. and remembering if the last one is number or not. if the former was number, but not, then put a comma.

String.substring has two ways to be used. In one, it receives a single parameter, which is the index from which to take the data, and it takes from there to the end of the chain. In its other version, of two parameters, the first is the start index, and the second the end index for the chain to be returned.

    
answered by 28.10.2017 / 08:46
source
2

Well, what I understand is basically separating non-digit digits (letters / signs). Since you are trying with regular expressions then the next one that looks for numeric and non-numeric patterns in any order can be useful:

String[] salida = result.split( "(?<=\D)(?=\d)|(?<=\d)(?=\D)" );
System.out.println( String.join( ",", salida ) );

Edit:

  • The meta-character \d Equivalent to numeric character (digit).
  • The meta-character \D Equivalent to non-numeric character (letters, signs).

In the RegExp logical operator | (or) logically it suggests that one action or another is going to be executed. On the one hand we have (?<=\D)(?=\d) where (?<=\D) ( Lookbehind Assertion ) we will indicate the coincidences that are preceded by a non-numeric character and (?=\d) ( Lookahead Assertion ) does not bring the patterns that are followed by a numeric character.

On the other hand we have (?<=\d)(?=\D) , to disaggregate see inversely than (?<=\d) ( Lookbehind Assertion ) gets us the patterns that are preceded by a digit and finally (?=\D) ( Lookahead Assertion ) will return matches followed by a letter.

In such a way that if for example String result = "+34/5+455-6*344343ABc232"; we would have as a result:

  

+, 34, /, 5, +, 455, -, 6, *, 344343, ABc, 232

    
answered by 28.10.2017 в 10:34