File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes split() method in String API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "split() method in String API" Watch "split() method in String API" New topic
Author

split() method in String API

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 230
hi guys
this is my main method code


the output is


if i replace the my main method code with this


my output looks like


my Question is why split method is not functioning propery with String two[]= te.split("|");
looking for your replies
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The argument to split is a regular expression; a kind of pattern to match against. "|" is a special character in regular expressions. To use it as a plain character, you must escape it with a backslash; to make sure the regular expression parser sees the backslash, you must double it. So the proper syntax is going to be:

String two[]= te.split("\\|");


[Jess in Action][AskingGoodQuestions]
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 230
thanks for your reply Ernest
but my doubt still exist

in the above case my split pattern dosent match in the string te


but in the above even though the split pattern dosent match why the characters are splited and return as an array

looking for your replies
amir
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

I still see this line in your code:

String two[]= te.split("|");

instead of what Ernest suggested:

String two[]= te.split("\\|");

Did you try this?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3165
    
  10
Originally posted by Amirtharaj Chinnaraj:

but in the above even though the split pattern dosent match why the characters are splited and return as an array


The | is a special character in regular expressions that means match either the part on the left side or the part on the right side of the |. As you don't have anything on either the left or right of the |, I presume (I'm not a RE expert) it is interpreted as meaning match anything, so you get back an array with each element being a single character string.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Originally posted by Joanne Neal:


The | is a special character in regular expressions that means match either the part on the left side or the part on the right side of the |. As you don't have anything on either the left or right of the |, I presume (I'm not a RE expert) it is interpreted as meaning match anything, so you get back an array with each element being a single character string.
More likely it is interpreted as "match nothing or nothing". You get a "nothing" after every letter, so it matches that and then goes to the next letter. That is why it splits the String into individual single letters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split() method in String API
 
Similar Threads
Splitting a string of asterisks
TreeSet with compartor constructor
Please help in this selection sort
Adding Arrays
Problem in StringTokenizer