Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
How can I split a string without knowing the split characters a-priori?

For my project I have to read various input graphs. Unfortunately, the input edges have not the same format. Some of them are comma-separated, others are tab-separated, etc. For example:

File 1:

123,45
67,89
...

File 2

123    45
67    89
...

Rather than handling each case separately, I would like to automatically detect the split characters. Currently I have developed the following solution:

String str = "123,45";
String splitChars = "";
for(int i=0; i < str.length(); i++) {
    if(!Character.isDigit(str.charAt(i))) {
      splitChars += str.charAt(i);
   }
}
  
String[] endpoints = str.split(splitChars);

Basically I pick the first row and select all the non-numeric characters, then I use the generated substring as split characters. Is there a cleaner way to perform this?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Split requires a regexp, so your code would fail for many reasons: If the separator has meaning in regexp (say, +), it'll fail. If there is more than 1 non-digit character, your code will also fail. If you code contains more than exactly 2 numbers, it will also fail. Imagine it contains hello, world - then your splitChars string becomes " , " - and your split would do nothing (that would split the string "test , abc" into two, nothing else).

Why not make a regexp to fetch digits, and then find all sequences of digits, instead of focussing on the separators?

You're using regexps whether you want to or not, so let's make it official and use Pattern, while we are at it.

private static final Pattern ALL_DIGITS = Pattern.compile("\\d+");

// then in your split method..

Matcher m = ALL_DIGITS.matcher(str);
List<Integer> numbers = new ArrayList<Integer>();
// dont use arrays, generally. List is better.
while (m.find()) {
    numbers.add(Integer.parseInt(m.group(0)));
}

//d+ is: Any number of digits.

m.find() finds the next match (so, the next block of digits), returning false if there aren't any more.

m.group(0) retrieves the entire matched string.

over 4 years ago · Santiago Trujillo Denunciar

0

Why not split with [^\d]+ (every group of nondigfit) :

    for (String n : "123,456 789".split("[^\\d]+")) {
        System.out.println(n);
    }

Result:

123
456
789
over 4 years ago · Santiago Trujillo Denunciar

0

Split the string on \\D+ which means one or more non-digit characters.

Demo:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "123,45", "67,89", "125      89", "678 129" };
        for (String s : arr) {
            System.out.println(Arrays.toString(s.split("\\D+")));
        }
    }
}

Output:

[123, 45]
[67, 89]
[125, 89]
[678, 129]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda