Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 833397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:30:42+00:00 2026-05-15T04:30:42+00:00

I need to split a text using the separator . . For example I

  • 0

I need to split a text using the separator ". ". For example I want this string :

Washington is the U.S Capital. Barack is living there.

To be cut into two parts:

Washington is the U.S Capital. 
Barack is living there.

Here is my code :

// Initialize the tokenizer
StringTokenizer tokenizer = new StringTokenizer("Washington is the U.S Capital. Barack is living there.", ". ");
 while (tokenizer.hasMoreTokens()) {
      System.out.println(tokenizer.nextToken());

}

And the output is unfortunately :

Washington
is
the
U
S
Capital
Barack
is
living
there

Can someone explain what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:30:43+00:00Added an answer on May 15, 2026 at 4:30 am

    Don’t use StringTokenizer; it’s a legacy class. Use java.util.Scanner or simply String.split instead.

        String text = "Washington is the U.S Capital. Barack is living there.";
        String[] tokens = text.split("\\. ");
        for (String token : tokens) {
            System.out.println("[" + token + "]");
        }
    

    This prints:

    [Washington is the U.S Capital]
    [Barack is living there.]
    

    Note that split and Scanner are “regex”-based (regular expressions), and since . is a special regex “meta-character”, it needs to be escaped with \. In turn, since \ is itself an escape character for Java string literals, you need to write "\\. " as the delimiter.

    This may sound complicated, but it really isn’t. split and Scanner are much superior to StringTokenizer, and regex isn’t that hard to pick up.

    Regular expressions tutorials

    • Java Lessons/Regular expressions
    • regular-expressions.info – Very good tutorial, not Java specific

    Related questions

    • Scanner vs. StringTokenizer vs. String.Split

    API Links

    • java.util.StringTokenizer
      • 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.
    • java.util.Scanner
      • A simple text scanner which can parse primitive types and strings using regular expressions.
      • Java Tutorials – Basic I/O – Scanning and formatting
    • String[] String.split
      • Splits this string around matches of the given regular expression.

    But what went wrong?

    The problem is that StringTokenizer takes each character in the delimiter string as individual delimiters, i.e. NOT the entire String itself.

    From the API:

    StringTokenizer(String str, String delim): 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file with some text information and i need to split this
I need split string by commas and spaces, but ignore the inside quotes, single
I need to split a keyword string and turn it into a comma delimited
I need to split my string input into an array at the commas. Is
I need to split a string into chunks of 2,2,3,3 characters and was able
I need to split the string result of the autocomplete plugin . I know
I need to split this: a:6:{s:8:address1;s:11:2 Bourke St;s:8:address2;s:13:Woolloomooloo;s:4:city;s:6:Sydney;s:5:state;s:3:NSW;s:11:postal_code;s:4:2011;s:7:country;s:9:Australia;} by ; to pull out the
I need to segment text using fullstops using PHP/Javascript.The problem is if I use
Hi i was trying to filter a large of text file using Regex.Split with
I'm using VS 2008 and need to read text files that have UTF-8 Chinese

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.