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

244
Visualizações
Why this .split resulting in array out of bound error?
public class test {
    public static void main(String[] args) {        
        String[] arr = {"0 1.2.3.4","a b.c.d.e"};
        System.out.println(arr[0].split(".")[2]);
    }
}

I am using java 8.

the expected output is 3.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The argument to split is a regular expression, not a literal 'this is the string you should scan for'. The . symbol in regex means 'anything', so it's like " ".split(" ") - the string you're passing is all separators, hence, you get no output.

.split(Pattern.quote(".")) will take care of it.

EDIT: To go in some detail - given that the string consists of all separators, split initially provides you with an array filled with empty strings; one for each position 'in between' any character (as each character is a separator). However, by default split will strip all empty strings off of the end, hence you end up with a 0-length string array.

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