Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!