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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:22:24+00:00 2026-06-07T06:22:24+00:00

Currently I am splitting a string by spaces. However there are some double spaces

  • 0

Currently I am splitting a string by spaces. However there are some double spaces that I want to preserve when I put them all back together. Any suggestions on how to do this?

I.e. the string "I went to the beach. I ate pie" is getting split as

I
went
to
the
beach.

I
ate
pie

I don’t want the blank entries but I want to put it back together to the same format. Thanks all!

  • 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-07T06:22:27+00:00Added an answer on June 7, 2026 at 6:22 am

    Do a String replaceAll(” “, ” unlikelyCharacterSequence”) and then split your string by spaces as normal. Then you can convert back to a double space by replacing your {unlikelyCharacterSequence} with ” ” at the end.

    However: this will fail if you ever encounter your “unlikely” character sequence in your actual, unmodified String. For a more general purpose solution, check the alternative listed below this example.

    Example (warning, depends on non-existance of !@#!@# :

    String example = "Hello.  That was a double space. That was a single space."
    String formatted = example.replace("  ", " !@#!@#");
    String [] split = formatted.split(" ");
    for(int i = 0; i < split.length; i++)
    {
      split.replace("!@#!@#", " ");
    }
    // Recombine your splits?
    

    Alternatively you could take a more robust strategy of recombining the string as you have it in your question but ignoring elements containing only a single space:

    String example = "ThisShouldBeTwoElements.  ButItIsNot.";
    String [] splitString = example.split(" ");
    String recombined = "";
    for(int i = 0; i < splitString.length; i++)
    {
      if(!splitString[i].equals(" "))
        recombined += splitString[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently looking into spliting a very long string that could contain HTML
I am working on splitting out an existing, working application that I currently have
I have a string like: {A}{B}={C}{D}<{E}{F}<= What I want to do is split that
I am currently receiving a string like this: @Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54 And I am splitting it
I am currently working on a project that involves splitting a url. I have
Currently i am splitting a string by pattern, like this: outcome_array=the_text.split(pattern_to_split_by) The problem is
I am currently splitting a string at every occurrence of a forward slash. In
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
I am currently trying to convert a VB6 program into C#. There was extensive

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.