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

256
Vistas
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 Respuestas
Responde la pregunta

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 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