• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split() method

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Can anyone expalin why the below codes create an empty output ??

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a regular expression, the period will match any character, so you're ending up with an array of length zero. (See java.util.regex.Pattern.) You need an escape sequence on the period...

String[] sel = s.split("\\.");
[ February 23, 2006: Message edited by: marc weber ]
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that I need it but I do not understand why I need it
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to escape symbols that have special meaning if you want to use the symbol without the meaning.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
You have to escape symbols that have special meaning if you want to use the symbol without the meaning.

Exactly. In a regular expression, the period is a "construct" with a special meaning of "match any character" (so every character in your String was a delimiter, and you ended up with nothing in between). See the Pattern link I posted above for other constructs.

By putting \\ in front of the period, you're saying, "I want this to be a literal period, with no special meaning."

As for why it's a double blackslash, note the following from the Pattern link posted above...
Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler.

[ February 23, 2006: Message edited by: marc weber ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do we use split method. Even with backslash the method is producing only one string as output(the entire string).

Regular Expression?

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your explanaitory replies...

Regards,

memati
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
Where do we use split method. Even with backslash the method is producing only one string as output(the entire string).

Regular Expression?




the output is :

today
the
weather
is
beautiful
 
Nelly Verccety
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have too much stuff to learn
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, was using forward slash.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nelly Verccety:
I have too much stuff to learn


On the other hand, some concepts are really useful and once learned go straight into the tool bag. Regular expressions are a good example of that. Well worth learning.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
... Regular Expression?

See this regular expressions tutorial.

Also see the package java.util.regex, and in particular the class java.util.regex.Pattern.
[ February 24, 2006: Message edited by: marc weber ]
 
Michael Hall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did Pierre De Fermat say? - in English please - thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Hall:
What did Pierre De Fermat say? - in English please - thanks

There's much more to it than the literal translation. See Wikipedia - Fermat's Last Theorem or Google the quote.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for link marc.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic