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 968849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:32:58+00:00 2026-05-16T02:32:58+00:00

I wish to have have the following String !cmd 45 90 An argument Another

  • 0

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.

EDIT:
I have changed the example yet again, this time I believe it explains the situation best although it is no different than the second example.

  • 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-16T02:32:59+00:00Added an answer on May 16, 2026 at 2:32 am

    It’s much easier to use a java.util.regex.Matcher and do a find() rather than any kind of split in these kinds of scenario.

    That is, instead of defining the pattern for the delimiter between the tokens, you define the pattern for the tokens themselves.

    Here’s an example:

        String text = "1 2 \"333 4\" 55 6    \"77\" 8 999";
        // 1 2 "333 4" 55 6    "77" 8 999
    
        String regex = "\"([^\"]*)\"|(\\S+)";
    
        Matcher m = Pattern.compile(regex).matcher(text);
        while (m.find()) {
            if (m.group(1) != null) {
                System.out.println("Quoted [" + m.group(1) + "]");
            } else {
                System.out.println("Plain [" + m.group(2) + "]");
            }
        }
    

    The above prints (as seen on ideone.com):

    Plain [1]
    Plain [2]
    Quoted [333 4]
    Plain [55]
    Plain [6]
    Quoted [77]
    Plain [8]
    Plain [999]
    

    The pattern is essentially:

    "([^"]*)"|(\S+)
     \_____/  \___/
        1       2
    

    There are 2 alternates:

    • The first alternate matches the opening double quote, a sequence of anything but double quote (captured in group 1), then the closing double quote
    • The second alternate matches any sequence of non-whitespace characters, captured in group 2
    • The order of the alternates matter in this pattern

    Note that this does not handle escaped double quotes within quoted segments. If you need to do this, then the pattern becomes more complicated, but the Matcher solution still works.

    References

    • regular-expressions.info/Brackets for Grouping and Capturing, Alternation with Vertical Bar, Character Class, Repetition with Star and Plus

    See also

    • regular-expressions.info/Examples – Programmer – Strings – for pattern with escaped quotes

    Appendix

    Note that StringTokenizer is a legacy class. It’s recommended to use java.util.Scanner or String.split, or of course java.util.regex.Matcher for most flexibility.

    Related questions

    • Difference between a Deprecated and Legacy API?
    • Scanner vs. StringTokenizer vs. String.Split
    • Validating input using java.util.Scanner – has many examples
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string from which I wish to extract a single word, but
For example, I have the following string: var string = 'watch this video http://vimeo.com/8122132
I have a multiple line string like following: END IF; EXECUTE IMMEDIATE ' CREATE
I have the following beans Task, ServerDetails and ApplicationDetails. I wish to retrieve all
I have the following method: public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) { return db.Profiles.Where(p
I wish to have long and short forms of command line options invoked using
Here is the domain that I wish to have: public class Person { public
If I have a non-template (i.e. normal) class and wish to have a template
I have a query where I wish to retrieve the oldest X records. At
I have a 3d object that I wish to be able to rotate around

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.