aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String.split( ) very confusing !!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String.split( ) very confusing !!!!!" Watch "String.split( ) very confusing !!!!!" New topic
Author

String.split( ) very confusing !!!!!

Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Hi All
I am getting totally unexpected output for below code .


Here the output is
abc
def
ghi
khl
abc$$def$$ghi$$khl but i was expecting as
abc
def
ghi
khl

abc
def
ghi
khl .Could you tell what is the problem if we specify the regex string as "$$" .

Thanks & Regards,
Prabhat
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Could you tell what is the problem if we specify the regex string as "$$"


The "$" has special mean to a regex -- it means the end of the input. To disable the meaning, you need to escape it.

Try setting the regex to "\\$\\$".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Thanks Henry. I got it . Could you tell me that apart from $ what are others special characters???
Jair Rillo Junior
Ranch Hand

Joined: Aug 27, 2008
Posts: 114
The String.split() method can receive a REGEX expression as argument (and it's very powerfull).

http://java.sun.com/docs/books/tutorial/essential/regex/ here, you're gonna see all special characters used by regex in Java


Regards, Jair Rillo Junior
http://www.jairrillo.com/blog, SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 5.0, IBM SOA Associate (Test 664).
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Prabhat Gupta, please quote you source for the posted question.

I would also request the fellow rancher's to ask posters to quote their source whenever they find there isn't a source in a post. Sometimes the moderators are busy and may not be able to visit the forums regularly.
This is necessary to avoid real sun questions being posted in the forum.
I hope everybody appreciates the idea and acts on it.


Preparing for SCJP 6.0
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Paul Somnath ,
Do you think that we must mention the source for posted question ?? can we don't create or do experiment with question .

I feel that each question that is posted here need not to belong Sun real exam question .
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Yes it is mandatory to post the source for your question. This is a policy followed in JavaRanch. If you don't follow, your thread would be deleted !
Even if you experiment with some question, you need to quote that too.
This is a policy and anyone posting questions needs to adhere to it.
Otherwise think of it, how would anyone know whether it is a real exam question or not?
There are enough unfair means going on and we don't want them to be included in this forum. Hope you understand.
Thanks.
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Even if you experiment with some question, you need to quote that too.


May be I missed that .It will be highly graceful to provide the pointer for same.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Prabhat If you have created the question yourself then it is no problem. If someone creates his/her own question, then they write something like this

"I was experimenting with split. I made this program but couldn't understand the output".

Hey this is not a rule to write this. But writing something of this sort will help moderators to understand that this your own code...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
I just wonder if someone wanting to post real exam questions posts with such a header.
How do we/moderators know actually that he is experimenting with questions or posting directly from the real exam?
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Again Saying, if some one is posting the real exam question he/she is not going to give heading as " SUN REAL EXAM QUESTION " .Secondly , Moderator are matured enough to know which are real exam question??

Finally ,if we are playing with code ,then it wont be silly to give the heading as "EXPERIMENT 1" Or "EXPERIMENT 2" . I feel here we are much interested to Heading of question rather approaching for solution .
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Originally posted by Prabhat Gupta:
I feel here we are much interested to Heading of question rather approaching for solution .

Its not like that. This is just to ensure that no one adopts unfair means. People come here and answer your questions for free. And mind you they don't get paid for that. You would find many distinguished engineers and authors hanging out in this forum. This is one of the most popular java forums on internet.
But since we are very much against unfair means, hence this policy. Some of the best people are here to solve your problems, but they expect you to be fair and honest and expect you to have tried on the problem that you are posting.
Hope you get the point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.split( ) very confusing !!!!!