• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. I got it . Could you tell me that apart from $ what are others special characters???
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
The moustache of a titan! The ad of a flea:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic