sgf8058
asked on
string partition
hi friends.I have a string.
abc || def||ghi
i want that it should get partiioned such that
abc is assigned to one string
def is assigned to other
.iam basically trying to divide string based on the sign ||.its very important.can some one kidly tell me the code for it.thanks
abc || def||ghi
i want that it should get partiioned such that
abc is assigned to one string
def is assigned to other
.iam basically trying to divide string based on the sign ||.its very important.can some one kidly tell me the code for it.thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
^ where line is your String.
in your case, better off with a StringTokenizer, i imagine, but you get the idea.
in your case, better off with a StringTokenizer, i imagine, but you get the idea.
This a school example. You can see what is going behind the scene!
import java.util.*;
public class Test
{
public static void main(String[] args)
{
if(args.length<2)
{
System.out.println("Usage: java Test [string_to_parse] [delimiter]");
System.exit(1);
}
int pos;
String s=args[0];
String delimiter=args[1];
Vector v=new Vector();
Enumeration e;
while((pos=s.indexOf(delim iter))!=-1 )
{
v.add(s.substring(0,pos));
s=s.substring(pos+delimite r.length() );
}
if(s.length()>0)
v.add(s);
e=v.elements();
while(e.hasMoreElements())
{
System.out.println((String )e.nextEle ment());
}
}
}
import java.util.*;
public class Test
{
public static void main(String[] args)
{
if(args.length<2)
{
System.out.println("Usage:
System.exit(1);
}
int pos;
String s=args[0];
String delimiter=args[1];
Vector v=new Vector();
Enumeration e;
while((pos=s.indexOf(delim
{
v.add(s.substring(0,pos));
s=s.substring(pos+delimite
}
if(s.length()>0)
v.add(s);
e=v.elements();
while(e.hasMoreElements())
{
System.out.println((String
}
}
}
objects- nice. i was writing while you posted.
StringTokenizer is perfect. But, it does appear his delimiter is double piped. To be exact, I suppose you could just change
>>> StringTokenizer st = new StringTokenizer(s, "|");
to StringTokenizer st = new StringTokenizer(s, "||" );
in case the data had a singe pipe in it.
StringTokenizer is perfect. But, it does appear his delimiter is double piped. To be exact, I suppose you could just change
>>> StringTokenizer st = new StringTokenizer(s, "|");
to StringTokenizer st = new StringTokenizer(s, "||" );
in case the data had a singe pipe in it.
functionpointer, calm down, this is a game!
Stringtokenizer st = new Stringtokenizer("||")
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();
a game..
A Game?
Programming IS A GAME TO YOU!?!?!??????
WHAT KINDA SICK, TWISTED...
oh. yeah, so it is. ok. ;-)
A Game?
Programming IS A GAME TO YOU!?!?!??????
WHAT KINDA SICK, TWISTED...
oh. yeah, so it is. ok. ;-)
umangjoshi,
I'd suggest testing that code :)
I'd suggest testing that code :)
Stringtokenizer st = new Stringtokenizer("||")
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();
> to StringTokenizer st = new StringTokenizer(s, "||" );
that won't make any difference.
that won't make any difference.
ok object
sorry
sorry
functionpointer,
you need a doctor!
you need a doctor!
ok object
sorry
sorry
>>>Stringtokenizer st = new Stringtokenizer("||")
simply useless .. no difference in one characters and two same characters..
simply useless .. no difference in one characters and two same characters..
the only problem is that .. if the sgf8058 wants to use "||" to differentiate from potential "|" character in his token .. like this
"abc|stillpart of abc||def |still part of def||efgh"
in that case he must not use StringTokenizer.
"abc|stillpart of abc||def |still part of def||efgh"
in that case he must not use StringTokenizer.
to tokenize on "word-delimiters" he will need to implement his own StringTokenizer_WordDelimi ter
And what I've written before? That is the answer!
at jdk 1.4 can use the follow code:
String s = "abc || def||ghi";
String[] ss = s.split("[|][|]",-1);
ss[] is your need.
how to use split() can read jdk document.
String s = "abc || def||ghi";
String[] ss = s.split("[|][|]",-1);
ss[] is your need.
how to use split() can read jdk document.
objects' code is correct.
There seems to be a bit of confusion here:
StringTokenizer st = new StringTokenizer(s, "|")
is exactly the same as
StringTokenizer st = new StringTokenizer(s, "||");
as the second argument does not function as a string, but as a set of characters, and is functionally equivalent to a regular expression character class (although from the fact ranges cannot be represented).
If you are not interested in the whitespace sgf8058, then you should trim it. Your first token for instance would have a space on the end otherwise.
There seems to be a bit of confusion here:
StringTokenizer st = new StringTokenizer(s, "|")
is exactly the same as
StringTokenizer st = new StringTokenizer(s, "||");
as the second argument does not function as a string, but as a set of characters, and is functionally equivalent to a regular expression character class (although from the fact ranges cannot be represented).
If you are not interested in the whitespace sgf8058, then you should trim it. Your first token for instance would have a space on the end otherwise.
This is a generic class MyStringTokenizer:
import java.util.*;
public class MyStringTokenizer
{
Enumeration tokens_enum;
Vector tokens;
public MyStringTokenizer(String str,String delim)
{
int pos;
tokens=new Vector();
while((pos=str.indexOf(del im))!=-1)
{
tokens.add(str.substring(0 ,pos));
str=str.substring(pos+deli m.length() );
}
if(str.length()>0)
tokens.add(str);
tokens_enum=tokens.element s();
}
public boolean hasMoreTokens()
{
return tokens_enum.hasMoreElement s();
}
public String nextToken()
{
return (String)tokens_enum.nextEl ement();
}
}
public class Test
{
public static void main(String[] args)
{
if(args.length<2)
{
System.out.println("Usage: java Test [string_to_parse] [delimiter]");
System.exit(1);
}
MyStringTokenizer st=new MyStringTokenizer(args[0], args[1]);
while(st.hasMoreTokens())
{
System.out.println(st.next Token());
}
}
}
You can now test it:
java Test ab!=cd!=ef !=
import java.util.*;
public class MyStringTokenizer
{
Enumeration tokens_enum;
Vector tokens;
public MyStringTokenizer(String str,String delim)
{
int pos;
tokens=new Vector();
while((pos=str.indexOf(del
{
tokens.add(str.substring(0
str=str.substring(pos+deli
}
if(str.length()>0)
tokens.add(str);
tokens_enum=tokens.element
}
public boolean hasMoreTokens()
{
return tokens_enum.hasMoreElement
}
public String nextToken()
{
return (String)tokens_enum.nextEl
}
}
public class Test
{
public static void main(String[] args)
{
if(args.length<2)
{
System.out.println("Usage:
System.exit(1);
}
MyStringTokenizer st=new MyStringTokenizer(args[0],
while(st.hasMoreTokens())
{
System.out.println(st.next
}
}
}
You can now test it:
java Test ab!=cd!=ef !=
>>>And what I've written before? That is the answer!
----------
for msterjev
----------
my comment was to support your and objects' views and to oppose function poniter views..
dont get too excited for posting a full source code for simple homework problem .. any one could have given that(including function pointer.. though he missed a point) ..
we here follow certain rules .. which includes not posting full-sources for home-work problems..
look how concise objects have been..
----------
for msterjev
----------
my comment was to support your and objects' views and to oppose function poniter views..
dont get too excited for posting a full source code for simple homework problem .. any one could have given that(including function pointer.. though he missed a point) ..
we here follow certain rules .. which includes not posting full-sources for home-work problems..
look how concise objects have been..
...including function pointer.. though he missed a point) ..
I like this. Sorry, if I've done some wrong,I only want to help!
Regards
I like this. Sorry, if I've done some wrong,I only want to help!
Regards
It's certainly useful for getting around the String/character class delimiter thing msterjev in JDK < 1.4. Whether that's relevant to this *particular* question is another thing ;-)
Ok, let's close this question, sgf8058 has already parse the whole hard disk :-)
ASKER
thanks everyone including function pointer,objects
oppose my views?
I was just trying to point out the diffence in the delimiter and not be rude to objects.
maybe my view was just a typo >> function poniter
I was just trying to point out the diffence in the delimiter and not be rude to objects.
maybe my view was just a typo >> function poniter
> and not be rude to objects.
I took no offence from your comment :)
I took no offence from your comment :)
sorry if any of my comment offeded any of you .. if it did .. that must have been my bad english :)
do something like:
int token;
Vector v = new Vector();
StreamTokenizer tok = new StreamTokenizer( new StringReader( line ) );
tok.wordChars( 0, 255 );
tok.ordinaryChar( '|' );
then you do something like
while (( token = tok.nextToken() ) != StreamTokenizer.TT_EOF )
{
if ( token == StreamTokenizer.TT_WORD ) v.addElement( tok.sval );
}
then pull the values out of your vector and assign them to whatever you like.