0

I would like to exclude full stop . from string tokens like in the following case:

 I go to school.

Would like to generate tokens as:

        [ I] [go] [to] [school]

I used stringTokenizer as:

List<String> listStr = new ArrayList<String>();
StringTokenizer strToken = new StringTokenizer(str);                    
int i =0;
while(strToken.hasMoreTokens()){
    listStr.add(i, strToken.nextToken());
    i=i+1;
}   

But the last token is school. which i don;t want. How would i do it?

thetna
  • 6,903
  • 26
  • 79
  • 113
  • using a regex with `replace()` or `replaceAll()` method should do, or have I misunderstood the questions? – posdef May 08 '12 at 13:26
  • 2
    Btw, you increment `i` is not needed. Just use the `add(Object)` and it will automatically be added to the end of your list – Guillaume Polet May 08 '12 at 13:26

4 Answers4

5

Isn't this simpler?

List<String> list = Arrays.asList("I go to school.".split("[ .]"));

System.out.println(list) prints

[I, go, to, school]
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • 1
    Yes it is simpler. Else, if he/she wants to keep the StringTokenizer, use this constructor: `new StringTokenizer(str," \t\f\r\n.");` – Guillaume Polet May 08 '12 at 13:28
2

The StringTokenizer has a constructor that takes another argument: The characters that act as delimiters. The following code illustrates this:

StringTokenizer tokenizer = new StringTokenizer("I go to school.", " \t\r\n.");
while(tokenizer.hasMoreElements()) {
    System.out.println(tokenizer.nextElement());
}

This is the output, which should be what you want:

I
go
to
school
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
rolve
  • 10,083
  • 4
  • 55
  • 75
1

You can do this on the initial string: string.replace(".", "") then do the tokenizer.

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
1

StringTokenizer can take a lot of delimiters, so now in your case you should build the strink tokenizer the following way:

new StringTokenizer(yourString, " ."); 

And thats it.

Hope it helps.