• Post Reply Bookmark Topic Watch Topic
  • New Topic

Split Head Scratcher  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I need some new eyes as I must be missing something stupid since this is so trivial.

The output of this is:
11.0.2.1
[]

How is that possible? Even if there were no '.' in the string it should contain the entire string as the first and only element of the array, right?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The split() method takes a regular expression. A DOT in regex is any character. So, in your example, every character in the string is a delimiter.

Additionally, you are using the version of the split() method, that deletes trailing blank matches. And since every character is a delimiter, then every match is a zero-length blank match.

Henry
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrgh! I knew it was something stupid. Thanks.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to escape the "." if you want it to be taken as the literal character.  Try split("\\.")

EDIT: Looks like you already did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!