• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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
0
sgf8058
Asked:
sgf8058
  • 7
  • 5
  • 5
  • +5
1 Solution
 
objectsCommented:
StringTokenizer st = new StringTokenizer(s, "|");
String a = st.nextToken();
String b = st.nextToken();
String c = st.nextToken();

0
 
functionpointerCommented:
Check out java.util.StreamTokenizer or java.util.StringTokenizer

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.
0
 
functionpointerCommented:
^ where line is your String.


in your case, better off with a StringTokenizer, i imagine, but you get the idea.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
msterjevCommented:
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(delimiter))!=-1)
          {
               v.add(s.substring(0,pos));
               s=s.substring(pos+delimiter.length());
          }
          if(s.length()>0)
               v.add(s);
          e=v.elements();
          while(e.hasMoreElements())
          {
               System.out.println((String)e.nextElement());
          }
     }
}
0
 
functionpointerCommented:
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.
0
 
msterjevCommented:
functionpointer, calm down, this is a game!
0
 
umangjoshiCommented:
Stringtokenizer st = new Stringtokenizer("||")
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();

0
 
functionpointerCommented:
a game..
 A Game?
   Programming IS A GAME TO YOU!?!?!??????
WHAT KINDA SICK, TWISTED...

oh. yeah, so it is. ok. ;-)
0
 
objectsCommented:
umangjoshi,

I'd suggest testing that code :)
0
 
umangjoshiCommented:
Stringtokenizer st = new Stringtokenizer("||")
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();

0
 
objectsCommented:
> to StringTokenizer st = new StringTokenizer(s, "||" );

that won't make any difference.
0
 
umangjoshiCommented:
ok object

sorry
0
 
msterjevCommented:
functionpointer,
you need a doctor!
0
 
umangjoshiCommented:
ok object

sorry
0
 
akshayxxCommented:
>>>Stringtokenizer st = new Stringtokenizer("||")
simply useless .. no difference in one characters and two same characters..



0
 
akshayxxCommented:
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.
0
 
akshayxxCommented:
to tokenize on "word-delimiters" he will need to implement his own StringTokenizer_WordDelimiter
0
 
msterjevCommented:
And what I've written before? That is the answer!
0
 
gwang77Commented:
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.
0
 
CEHJCommented:
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.

0
 
msterjevCommented:
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(delim))!=-1)
          {
               tokens.add(str.substring(0,pos));
               str=str.substring(pos+delim.length());
          }
          if(str.length()>0)
               tokens.add(str);
          tokens_enum=tokens.elements();
         
     }
     
     public boolean hasMoreTokens()
     {
          return tokens_enum.hasMoreElements();
     }
     
     public String nextToken()
     {
          return (String)tokens_enum.nextElement();
     }
}

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.nextToken());
          }
     }
}


You can now test it:

java Test ab!=cd!=ef !=
0
 
akshayxxCommented:
>>>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..
0
 
msterjevCommented:
...including function pointer.. though he missed a point) ..

I like this. Sorry, if I've done some wrong,I only want to help!

Regards
0
 
CEHJCommented:
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 ;-)
0
 
msterjevCommented:
Ok, let's close this question, sgf8058 has already parse the whole hard disk :-)
0
 
sgf8058Author Commented:
thanks everyone including function pointer,objects
0
 
functionpointerCommented:
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
0
 
objectsCommented:
> and not be rude to objects.

I took no offence from your comment :)
0
 
akshayxxCommented:
sorry if any of my comment offeded any of you .. if it did .. that must have been my bad english :)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 7
  • 5
  • 5
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now