• Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting a string  RSS feed

 
AbhisheK Aggarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a a string of the form
I have to break it up first by | and then by ; . then i have to add its contents to vector..



but the out is coming like
b
h
;
|
a
s

and so on..
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by AbhisheK Aggarwal:




but the out is coming like
b
h
;
|
a
s

and so on..


This is because the "|" is an OR operator . Logical operator "|", used to compile pattern , either "A" or "B" , and passed as an argument like this "A|B" , so in your case , you are saying something like this :

Either compile "" this pattern OR(|) "" pattern ,

(String#split() method works on Pattern and Matcher class , internally ) ..

I may not be 100 % correct here, so any one , please correct me !!

[ July 07, 2008: Message edited by: Sagar Rohankar ]
[ July 07, 2008: Message edited by: Sagar Rohankar ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar is right - you need to escape the pipe symbol:

String aliasArray[] = saveString.split("\\|"); //splitting by shifts
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!