How does the java " stringtokenizer.nextToken(delimiter); " work?
What i mean is what string values are accepted as delimiters? this is because i keep trying to use a string composed of several different characters but the program just seems to ignore it as just scan with empty space as the default delimiter...
For example if the tokenized string is as follows: Phone Number = 790-3233
I would like开发者_高级运维 the 1st token to be up to the " = " thus i set it as the delimiter and the next token should just be the string "790-3233"
Thanks in advance...
By Default the Delimiters is space
, if you do not supply one
// Extracted StringTokenizer.java
public StringTokenizer(String string)
{
this(string, " \t\n\r\f", false);
}
If you supply =
as delimiter along with the string then it splits
StringTokenizer st = new StringTokenizer("Phone Number = 790-3233","=");
Following the remark from the API docs:
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.
I'd use split in this case:
String text = "Phone Number = 790-3233";
String[] tokens = text.split("\\s*=\\s*");
The regex \s*=\s*
matches zero or more space-chars, followed by an =
sign, followed by zero or more space-chars.
Use :
String[] result = "your string=790-3233".split("=");
For string Tokenizer, see reference.
精彩评论