The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

I don't know...

Posted by Kishori Sharan on October 09, 2000 at 4:03 PM

Hi
I don't know if there is any utility class in java which will help you . However, I have created a new class for you which will certainly help you getting what you want. The class name is Test and it works exactly as StringTokenizer class works, but it uses the whole char set as delimiter. You have to create it like
Test t = new Test ( your_string, your_optional_delimiter ) ;

If you don't use any delimiter then a space is used as a delimiter. Then you work with this as

while ( t.hasMoreTokens ( ) {

String token = t.nextToken ( ) ;
}
Note: If your string starts with the delimiter itself then it considers a token of empty string between the start of the string and the first occurrance of delimiter which is also at the start of the string. If your delimiter string is placed once after another then it considers one token of empty string between them. You can also rest the delimiter in between calls to nextToken ( ) method using setDelimiter ( ) method. You can also just start over the iteration by just calling the rest ( ) method.
For more explanation see the comments within the code. Change the class name and its constrctor names and you can use it. If you need some more functionaliy just customize it.

Thanx
Kishori

//////////////// Test.java

public class Test {

private String str = "" ;
private String delim = " " ; // default delimiter is a space
private int currPos = 0 ;

public Test ( String str, String delim ) {
this.str = str ;

// Make sure there is a valid delimiter, otherwise use space as delimiter
if ( delim != null && delim.length ( ) > 0 ) {
this.delim = delim ;
}
}

public Test ( String str ) {
this.str = str ;
}


public static void main ( String[] args ) {
String str = "~#~#~#~#" ; //Hello~#~#I want to ~#break this string~into#some~#pieces~#delimited by~#" ;
String delim = "~#" ;
String temp = "";
Test t = new Test ( str, delim ) ;
System.out.println ( "String is : " + str ) ;
int counter = 0 ;
while ( t.hasMoreTokens ( ) ) {
counter++;
temp = t.nextToken ( ) ;
System.out.println ( " Token no " + counter + " = " + temp ) ;
}

}

// Delimiter can be reset at nay time
public void setDelimiter ( String delim ) {
// Make sure there is a valid delimiter, otherwise use space as delimiter
if ( delim != null && delim.length ( ) > 0 ) {
this.delim = delim ;
}
}

// Starts the iteration from begining
public void reset ( ) {
// Reset the current position to zero
currPos = 0 ;
}


// Returns the next token
public String nextToken ( ) {

int pos1 = 0 ;
int delimLen = delim.length ( ) ;
String token = "" ;

if ( str == null ) {
return "";
}

if ( str.length ( ) == 0 ) {
return "" ;
}

// Get the position of the delimiter after the current position
pos1 = str.indexOf ( delim, currPos ) ;
if ( pos1 == -1 ) {
// This condition may occur when string
// doesn't have any delimiter in it or when we are at the last delimiter in string
token = str.substring ( currPos ) ;
currPos = str.length ( ) ;
return token ;
}


// Get the string between current position and the beginning of
// next delimiter i.e. pos1
token = str.substring ( currPos, pos1 ) ;

// Forward the currPos by the length of the delimiter
currPos = pos1 + delimLen ;

return token ;
}

//Returns if there is any more elements
public boolean hasMoreTokens ( ) {

/*** Logic used ***/
/* 1. If string is null or empty string then it cannot have any token
2. If current position is equal to or greater than the length of the
string then there cannot be any more token.
3. If the current position is just before the delimiter and after that
delimiter occurrance there is no character in the string then there
cannot be any more tokens.
*/

if ( str == null ) {
return false ;
}

if ( str.length( ) == 0 ) {
return false;
}

if ( currPos >= str.length ( ) ) {
return false;
}

// Get the last occurrance of the delimiter
int lastPos = str.lastIndexOf ( delim ) ;

if ( lastPos == -1 ) {
// It means there is no delimiter in the string and so it has
// at least one token
return true;
}

if ( currPos == lastPos ) {
// It means the current position is at the last occurrance of
// delimiter. We need to ckeck if there is only delimter characters after
// the current position
if ( ( currPos + delim.length ( ) ) == str.length ( ) ) {
return false;
}
}

return true ;
}

}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us