aspose file tools*
The moose likes Beginning Java and the fly likes split() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "split() method" Watch "split() method" New topic
Author

split() method

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hi everyone,
Can anyone expalin why the below codes create an empty output ??

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
I know that I need it but I do not understand why I need it
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You have to escape symbols that have special meaning if you want to use the symbol without the meaning.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Nov 15, 2004
Posts: 1026
Where do we use split method. Even with backslash the method is producing only one string as output(the entire string).

Regular Expression?



Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Thanks for your explanaitory replies...

Regards,

memati
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Nelly Verccety
Greenhorn

Joined: Feb 16, 2006
Posts: 8
I have too much stuff to learn
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
sorry, was using forward slash.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Dec 17, 2005
Posts: 21
What did Pierre De Fermat say? - in English please - thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Nov 15, 2004
Posts: 1026
Thanks for link marc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split() method