Jump to content

Problem with splitting

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Why doesn't this code work? Throws ArrayIndexOutOfBoundsException. Really weird, what am i missing?

        String s = "java.awt.Color.r.0.g.255.b.0.";
        String[] split = s.split(".");        
        System.out.println(split[0]);        


#2
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
It happens because String.Split() expects a regular expression pattern string as a parameter, And in regular expressions, the "." is a special character, so to tell regular expressions that you want to match the exact "." you just add an escape character so it looks like "\." But still won't work because you need another escape character for java (1 for regex and 1 for java string) so all you need is to replace s.split(".") with s.split("\\.").

        String s = "java.awt.Color.r.0.g.255.b.0.";
        String[] split = s.split("\\."); //regex delimiter        
        System.out.println(split[0]);


#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Thanks a lot, for explaining :)

#4
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
No problem. =)