Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tokenizer - can you use a string as a separator?

Status
Not open for further replies.

fjweaver

Programmer
Oct 18, 2002
11
0
0
GB
I want to go through a string, extracting all substrings in turn that are separated by a unique separator string (ie: ':mad::')
If I try using :mad:: as the separator it extracts data based on a separator of either ":" or "@".
Is there a way of doing this?
thanks
Frank
 
use the StringTokenizer class...

StringTokenizer sttoken = new StringTokenizer(theString, ":mad::");
while (sttoken.hasMoreElements()){
storeithere = (String)sttoken.nextElement();
}
 
Thanks for your reply - I tried that & it didn't work. I'm new to java so maybe I haven't done something correctly as it's taking either a ":" or "@" as a separator

String theString="AAAAA :mad:: BBBBB : CCCCC:DDDDD EEEEE:mad::FFFFF"
StringTokenizer sttoken = new StringTokenizer(theString, ":mad::");
while (sttoken.hasMoreElements()){
storeithere = (String)sttoken.nextElement();
}

the results I got from this code was:
1)"AAAAA "
2)" BBBBB "
3)" CCCCC"
4)"DDDDD EEEEE"
5)"FFFFF"

the results I would expect are:
1)"AAAAA "
2)" BBBBB : CCCCC:DDDDD EEEEE"
3)"FFFFF"


any ideas?
 
Good point.

StringTokenizer will look for ANY char in the delimeter String and break string apart that way.

A brute force way around would be to use indexOf(":mad::") and get substring something like...

ok .. this is from memory so don't grade me on the code writing...this should give the jist of a way to do this. keep in mind..kind of lots of overhead here dealing with substrings ...

public Vector getStrings(String masterString){

Vector values = new Vector();
int indexPos = 0;
int i = 0;

while((i = indexOf(":mad::", indexPos)) != -1){
values.addElement(masterString.subString(indexPos, i));
indexPos = i+3;//move index over three places
}

}
 
that's what I was trying to get away from but can't see much alternative than to do what you suggest

thanks a lot for your help
 
Another option is to use Regular Expressions...With is now a part of the java API (1.4.x)... for example:

Code:
import java.util.regex.*;

public class Splittext
{
	public static void main(String[] args) throws Exception
	{
		//The delimiter that you would like to use
		Pattern pattern = Pattern.compile(":@:");
		
		
		String theString=("AAAAA :@: BBBBB : CCCCC:DDDDD EEEEE:@:FFFFF");
		
		// The split will create a token based on the pattern
		String[] result = pattern.split(theString);
		
		//prints out the results to the console
		for (int i=0; i<result.length; i++)
		{
			System.out.println(result[i]);
		}
	}
}
[code]
**** The Output for the above code sample is ****

C:\>Java -cp . Splittext
AAAAA
 BBBBB : CCCCC:DDDDD EEEEE
FFFFF
 
I'll take that back - it worked nicely under 1.4 but I need it at 1.3, any other suggestions?
 
I can not think of one...I'm sure there is probably another way though...however ksolos suggestion does seem to work.

If your project can allow you to use third party api, I would download the Jakarta-ORO Java classes; they are a set of text-processing Java classes that provide Perl5 compatible regular expressions, AWK-like regular expressions, glob expressions, and utility classes for performing substitutions, splits, filtering filenames, etc. Using this API use can create the code code...

You can find the API at
hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top