Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I have some friends making a text-based game in Java (what the hell?), and they're looking for the best way to parse strings for commands. They've come across many methods and are wondering what would be the best way to go about things.
How do I split strings in J2ME in an effective way? There is a StringTokenizer or String.split(String regex) in the standard edition (J2SE), but they are absent in the micro edition (J2ME, MIDP).
I'm searching for a library similar in functionality to the Perl Lingua::EN::NameParse module. Essentially, I'd like to parse strings like 'Mr. Bob R. Smith' into prefix, first name, last name, and name suffix components. Google hasn't been much help in finding something like this and I'd prefer not to roll my own if possible. Anyone know of a OSS Java library that can do this in a sophistica...
I have this string (Java 1.5): :alpha;beta:gamma;delta I need to get an array: {":alpha", ";beta", ":gamma", ";delta"} What is the most convenient way to do it in Java?
The Java documentation doesn't seem to mention anything about deprecation for StringTokenizer, yet I keep hearing about how it was deprecated long ago. Was it deprecated because it had bugs/errors, or is String.split() simply better to use overall? I have some code that uses StringTokenizer and I am wondering if I should seriously be concerned about refactoring it to use String.split(), or wh...
How would I parse for the word "hi" in the sentence "hi, how are you?" or in parse for the word "how" in "how are you?"? example of what I want in code: String word = "hi"; String word2 = "how"; Scanner scan = new Scanner(System.in).useDelimiter("\n"); String s = scan.nextLine(); if(s.equals(word)) { System.out.println("Hey"); } if(s.equals(word2)) { System.out.println("Hey"); }
I have this weird situation where I have to read horizontally. So I am getting a csv file which has data in horizontal format. Like below: CompanyName,RunDate,10/27/2010,11/12/2010,11/27/2010,12/13/2010,12/27/2010.... All the dates shown after RunDate are values for run date field and I have to update that field for that company in my system. The date values are not fix number, they can be s...
I wish to have have the following String !cmd 45 90 "An argument" Another AndAnother "Another one in quotes" to become an array of the following { "!cmd", "45", "90", "An argument", "Another", "AndAnother", "Another one in quotes" } I tried new StringTokenizer(cmd, "\"") but this would return "Another" and "AndAnother as "Another AndAnother" which is not the desired effect. Thanks. EDI...
Can someone tell me exactly what is this regex doing? new StringTokenizer(string,":/.@\\;,+ "); I could not understand the exact meaning of this Regex, though it looks like something to do with splitting a string based on special characters?
What's does Enumeration<?> mean? Is there any way to represent the general generic?
I have a String variable: String str = " abc boy abc funny os abc abcifff abc abc boy abc"; I want to replace "abc" as "the", which would result in: the boy the funny os the abcifff the the boy the How can I do this without using replace or replaceAll?
Maybe I am stupid but I don't understand why the behaviour of StringTokenizer here: import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; String object = (String) value; String escaped = escapeHtml(object); StringTokenizer tokenizer = new StringTokenizer(escaped, escapeHtml("<br/>")); If fx. value is Hej<br/>$user.get(0).name Har vundet<br/><table border...
I have a number of files and they are all called something like name_version_xyz.ext. In my Java code I need to extract the name and the version part of the filename. I can accomplish this using lastIndexOf where I look for underscore, but I don't think that's the nicest solution. Can this be done with a regexp somehow? Note that the "name" part can contain any number of underscores.
I need to be able to split an input String by commas, semi-colons or white-space (or a mix of the three). I would also like to treat multiple consecutive delimiters in the input as a single delimiter. Here's what I have so far: String regex = "[,;\\s]+"; return input.split(regex); This works, except for when the input string starts with one of the delimiter characters, in which case the ...
Hi All I am a newcomer here, I am using the following code from an open source library (Matrix Toolkits for Java) which outputs the following matrix 1000 1000 5 3 5 1.000000000000e+00 I am trying to do a String split that will return me 1000,1000,5 I tried using String[] parts = str.trim().split("\\s"); but it seems using the \s as a String Tok...
Here is my problem: Create a constructor for a telephone number given a string in the form xxx-xxx-xxxx or xxx-xxxx for a local number. Throw an exception if the format is not valid. So I was thinking to validate it using a regular expression, but I don't know if I'm doing it correctly. Also what kind of exception would I have to throw? Do I need to create my own exception? public Teleph...
This is a common task I'm facing: splitting a space separated list into a head element and an array containing the tail elements. For example, given this string: the quick brown fox We want: "the" ["quick","brown","fox"] .. in two different variables. The first variable should be a string, and the second an array. I'm looking for an elegant way to do this (preferably in Java).
Let's say have a string... String myString = "my*big*string*needs*parsing"; All I want is to get an split the string into "my" , "big" , "string", etc. So I try myString.split("*"); returns java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 * is a special character in regex so I try escaping.... myString.split("\\*"); same exception. I figured someone ...
Input: Hi. I am John. My name is John. Who are you ? Output: Hi I am John My name is John Who are you
I need to parse some text files that have different types of delimiters (tildes, spaces, commas, pipes, caret characters). There is also a different order of elements depending on what the delimiter is, e.g: comma: A, B, C, D, E caret: B, C, A, E, D tilde: C, A, B, D, E The delimiter is the same within the file but different from one file to another. From what I can tell, there are no delim...
Is there a nice way to extract tokens that start with a pre-defined string and end with a pre-defined string? For example, let's say the starting string is "[" and the ending string is "]". If I have the following string: "hello[world]this[[is]me" The output should be: token[0] = "world" token[1] = "[is" (Note: the second token has a 'start' string in it)
  /*
   * Copyright 1994-2004 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.util;
 
 import java.lang.*;

The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.

An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:

  • If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
  • If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a maximal sequence of consecutive characters that are not delimiters.

A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.

A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

The following is one example of the use of the tokenizer. The code:

     StringTokenizer st = new StringTokenizer("this is a test");
     while (st.hasMoreTokens()) {
         System.out.println(st.nextToken());
     }
 

prints the following output:

     this
     is
     a
     test
 

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

     String[] result = "this is a test".split("\\s");
     for (int x=0; x<result.length; x++)
         System.out.println(result[x]);
 

prints the following output:

     this
     is
     a
     test
 

Author(s):
unascribed
Since:
JDK1.0
See also:
java.io.StreamTokenizer
public
class StringTokenizer implements Enumeration<Object> {
    private int currentPosition;
    private int newPosition;
    private int maxPosition;
    private String str;
    private String delimiters;
    private boolean retDelims;
    private boolean delimsChanged;

    
maxDelimCodePoint stores the value of the delimiter character with the highest value. It is used to optimize the detection of delimiter characters. It is unlikely to provide any optimization benefit in the hasSurrogates case because most string characters will be smaller than the limit, but we keep it so that the two code paths remain similar.
    private int maxDelimCodePoint;

    
If delimiters include any surrogates (including surrogate pairs), hasSurrogates is true and the tokenizer uses the different code path. This is because String.indexOf(int) doesn't handle unpaired surrogates as a single character.
    private boolean hasSurrogates = false;

    
When hasSurrogates is true, delimiters are converted to code points and isDelimiter(int) is used to determine if the given codepoint is a delimiter.
    private int[] delimiterCodePoints;

    
Set maxDelimCodePoint to the highest char in the delimiter set.
    private void setMaxDelimCodePoint() {
        if ( == null) {
             = 0;
            return;
        }
        int m = 0;
        int c;
        int count = 0;
        for (int i = 0; i < .length(); i += Character.charCount(c)) {
            c = .charAt(i);
            if (c >= . && c <= .) {
                c = .codePointAt(i);
                 = true;
            }
            if (m < c)
                m = c;
            count++;
        }
         = m;
        if () {
             = new int[count];
            for (int i = 0, j = 0; i < counti++, j += Character.charCount(c)) {
                c = .codePointAt(j);
                [i] = c;
            }
        }
    }

    
Constructs a string tokenizer for the specified string. All characters in the delim argument are the delimiters for separating tokens.

If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.

Note that if delim is null, this constructor does not throw an exception. However, trying to invoke other methods on the resulting StringTokenizer may result in a NullPointerException.

Parameters:
str a string to be parsed.
delim the delimiters.
returnDelims flag indicating whether to return the delimiters as tokens.
Throws:
java.lang.NullPointerException if str is null
    public StringTokenizer(String strString delimboolean returnDelims) {
         = 0;
         = -1;
         = false;
        this. = str;
         = str.length();
         = delim;
         = returnDelims;
        setMaxDelimCodePoint();
    }

    
Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.

Note that if delim is null, this constructor does not throw an exception. However, trying to invoke other methods on the resulting StringTokenizer may result in a NullPointerException.

Parameters:
str a string to be parsed.
delim the delimiters.
Throws:
java.lang.NullPointerException if str is null
    public StringTokenizer(String strString delim) {
        this(strdelimfalse);
    }

    
Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.

Parameters:
str a string to be parsed.
Throws:
java.lang.NullPointerException if str is null
    public StringTokenizer(String str) {
        this(str" \t\n\r\f"false);
    }

    
Skips delimiters starting from the specified position. If retDelims is false, returns the index of the first non-delimiter character at or after startPos. If retDelims is true, startPos is returned.
    private int skipDelimiters(int startPos) {
        if ( == null)
            throw new NullPointerException();
        int position = startPos;
        while (! && position < ) {
            if (!) {
                char c = .charAt(position);
                if ((c > ) || (.indexOf(c) < 0))
                    break;
                position++;
            } else {
                int c = .codePointAt(position);
                if ((c > ) || !isDelimiter(c)) {
                    break;
                }
                position += Character.charCount(c);
            }
        }
        return position;
    }

    
Skips ahead from startPos and returns the index of the next delimiter character encountered, or maxPosition if no such delimiter is found.
    private int scanToken(int startPos) {
        int position = startPos;
        while (position < ) {
            if (!) {
                char c = .charAt(position);
                if ((c <= ) && (.indexOf(c) >= 0))
                    break;
                position++;
            } else {
                int c = .codePointAt(position);
                if ((c <= ) && isDelimiter(c))
                    break;
                position += Character.charCount(c);
            }
        }
        if ( && (startPos == position)) {
            if (!) {
                char c = .charAt(position);
                if ((c <= ) && (.indexOf(c) >= 0))
                    position++;
            } else {
                int c = .codePointAt(position);
                if ((c <= ) && isDelimiter(c))
                    position += Character.charCount(c);
            }
        }
        return position;
    }
    private boolean isDelimiter(int codePoint) {
        for (int i = 0; i < .i++) {
            if ([i] == codePoint) {
                return true;
            }
        }
        return false;
    }

    
Tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.

Returns:
true if and only if there is at least one token in the string after the current position; false otherwise.
    public boolean hasMoreTokens() {
        /*
         * Temporarily store this position and use it in the following
         * nextToken() method only if the delimiters haven't been changed in
         * that nextToken() invocation.
         */
        return ( < );
    }

    
Returns the next token from this string tokenizer.

Returns:
the next token from this string tokenizer.
Throws:
NoSuchElementException if there are no more tokens in this tokenizer's string.
    public String nextToken() {
        /*
         * If next position already computed in hasMoreElements() and
         * delimiters have changed between the computation and this invocation,
         * then use the computed value.
         */
         = ( >= 0 && !) ?
             : skipDelimiters();
        /* Reset these anyway */
         = false;
         = -1;
        if ( >= )
            throw new NoSuchElementException();
        int start = ;
        return .substring(start);
    }

    
Returns the next token in this string tokenizer's string. First, the set of characters considered to be delimiters by this StringTokenizer object is changed to be the characters in the string delim. Then the next token in the string after the current position is returned. The current position is advanced beyond the recognized token. The new delimiter set remains the default after this call.

Parameters:
delim the new delimiters.
Returns:
the next token, after switching to the new delimiter set.
Throws:
NoSuchElementException if there are no more tokens in this tokenizer's string.
java.lang.NullPointerException if delim is null
    public String nextToken(String delim) {
         = delim;
        /* delimiter string specified, so set the appropriate flag. */
         = true;
        setMaxDelimCodePoint();
        return nextToken();
    }

    
Returns the same value as the hasMoreTokens method. It exists so that this class can implement the Enumeration interface.

Returns:
true if there are more tokens; false otherwise.
See also:
Enumeration
hasMoreTokens()
    public boolean hasMoreElements() {
        return hasMoreTokens();
    }

    
Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface.

Returns:
the next token in the string.
Throws:
NoSuchElementException if there are no more tokens in this tokenizer's string.
See also:
Enumeration
nextToken()
    public Object nextElement() {
        return nextToken();
    }

    
Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. The current position is not advanced.

Returns:
the number of tokens remaining in the string using the current delimiter set.
See also:
nextToken()
    public int countTokens() {
        int count = 0;
        int currpos = ;
        while (currpos < ) {
            currpos = skipDelimiters(currpos);
            if (currpos >= )
                break;
            currpos = scanToken(currpos);
            count++;
        }
        return count;
    }
New to GrepCode? Check out our FAQ X