?
Solved

string partition

Posted on 2003-03-03
29
Medium Priority
?
209 Views
Last Modified: 2010-03-31
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
Comment
Question by:sgf8058
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 5
  • +5
29 Comments
 
LVL 92

Accepted Solution

by:
objects earned 380 total points
ID: 8062423
StringTokenizer st = new StringTokenizer(s, "|");
String a = st.nextToken();
String b = st.nextToken();
String c = st.nextToken();

0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8062446
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8062452
^ 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!

 
LVL 3

Expert Comment

by:msterjev
ID: 8062571
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8062593
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
 
LVL 3

Expert Comment

by:msterjev
ID: 8062620
functionpointer, calm down, this is a game!
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8062632
Stringtokenizer st = new Stringtokenizer("||")
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();

0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8062640
a game..
 A Game?
   Programming IS A GAME TO YOU!?!?!??????
WHAT KINDA SICK, TWISTED...

oh. yeah, so it is. ok. ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 8062646
umangjoshi,

I'd suggest testing that code :)
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8062656
Stringtokenizer st = new Stringtokenizer("||")
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();

0
 
LVL 92

Expert Comment

by:objects
ID: 8062657
> to StringTokenizer st = new StringTokenizer(s, "||" );

that won't make any difference.
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8062659
ok object

sorry
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8062671
functionpointer,
you need a doctor!
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8062690
ok object

sorry
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8062712
>>>Stringtokenizer st = new Stringtokenizer("||")
simply useless .. no difference in one characters and two same characters..



0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8062722
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8062733
to tokenize on "word-delimiters" he will need to implement his own StringTokenizer_WordDelimiter
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8062753
And what I've written before? That is the answer!
0
 
LVL 1

Expert Comment

by:gwang77
ID: 8062757
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063293
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
 
LVL 3

Expert Comment

by:msterjev
ID: 8063560
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8063606
>>>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
 
LVL 3

Expert Comment

by:msterjev
ID: 8063761
...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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063785
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
 
LVL 3

Expert Comment

by:msterjev
ID: 8063814
Ok, let's close this question, sgf8058 has already parse the whole hard disk :-)
0
 

Author Comment

by:sgf8058
ID: 8064592
thanks everyone including function pointer,objects
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8066104
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
 
LVL 92

Expert Comment

by:objects
ID: 8069633
> and not be rude to objects.

I took no offence from your comment :)
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8070554
sorry if any of my comment offeded any of you .. if it did .. that must have been my bad english :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question