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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:38:28+00:00 2026-06-09T22:38:28+00:00

I am splitting a string by word boundary. What I am expecting is: TOKEN

  • 0

I am splitting a string by word boundary.

What I am expecting is:

 TOKEN 0  
 TOKEN 1 0
 TOKEN 2  
 TOKEN 3 +Ve

and, what I am getting is,

 TOKEN 0  
 TOKEN 1 0
 TOKEN 2  +
 TOKEN 3 Ve



public void StringExample(){
    String str = " 0 +Ve";

    String[] token = str.split("\\b");

    System.out.println("TOKEN 0 " + token[0]);
    System.out.println("TOKEN 1 " + token[1]);
    System.out.println("TOKEN 2 " + token[2]);
    System.out.println("TOKEN 3 " + token[3]);
}

Can someone give a clue where its going wrong? and Possible corrections if any,

  • 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-06-09T22:38:30+00:00Added an answer on June 9, 2026 at 10:38 pm

    Nothing is going wrong, and the results are as should be expected. Word boundaries match at the before the first character of a String, after the last character of a String and between two characters in the string, where one is a word character and the other is not a word character. The last rule will result in a match between ‘+’ and ‘V’, and so your results make perfect sense.

    Perhaps you want to use look ahead and look behind to match anything next to a space. For example:

    public class Foo001 {
       // private static final String REGEX1 = "\\b";
       private static final String REGEX2 = "(?= )|(?<= )";
    
       public static void main(String[] args) {
          String str = " 0 +Ve";
    
          String[] tokens = str.split(REGEX2);
          for (int i = 0; i < tokens.length; i++) {
             System.out.printf("token %d: \"%s\"%n", i, tokens[i]);
          }
    
       }
    }
    

    This will also match the left of the first space giving an extra token:

    token 0: ""
    token 1: " "
    token 2: "0"
    token 3: " "
    token 4: "+Ve"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string 3/4Ton. I want to split it as --> word[1]
Hi I have simple split script and I am splitting a string on new
String.Split is convenient for splitting a string with in multiple part on a delimiter.
I want to solve word splitting problem (parse words from long string with no
Currently i am splitting a string by pattern, like this: outcome_array=the_text.split(pattern_to_split_by) The problem is
I found some table valued function for splitting string in sp and getting values
I've got some code splitting a camelCase string into a sentence where each word
I have a question about splitting string in javascript. I'm getting a string from
I am splitting a string into a vector of strings vector<string> tokens; stringstream strstm(str);
I'm seeking a solution to splitting a string which contains text in the following

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.