Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda