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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:26:18+00:00 2026-05-30T09:26:18+00:00

I would like to parse a string using whitespace as delimiters. I know it

  • 0

I would like to parse a string using whitespace as delimiters. I know it can be done using 3 methods from the string library, assign, substring, and find . Basically I want to assign my target string the substring from the beginning of the string to the first occurrences of whitespace.

It would be something like this

while(!line.empty())
{

    line.assign(line.substr(0, line.find(" ")
    line.erase(0, line.find(" ");

}

My question is, would this be a good way to go about parsing a string?

  • 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-30T09:26:19+00:00Added an answer on May 30, 2026 at 9:26 am

    You’d need a bit more than just one line variable: as is your code won’t terminate if the string contains a non-space character. Of course, this assumes that it compiles in the first place as there are a number of parenthesis and a semicolon missing (also, your question doesn’t match your code: there is no function substring; if you want to program effectively, you have to be very precise about everything: computer take your statements very literal). You probably want to chop your line up into its bits and put them into some sort of container.

    Once over these trivial aspect, you might want to consider using ' ' instead of " " because in this case the compiler and library can assume that you want to look for just one character rather than for a sequence of characters. This can be quite a bit faster. However, this is actually not whitespace but just space. Whitespace also includes a number of special characters like '\t' (tab), '\r' (carriage return), '\n' (newline), '\v' (vertical tab), '\b' (backspace), and '\f' (form feed). Speaking of speed you probably don’t want to erase() bits from the beginning of the string as this yield an O(n * n) algorithm while this can be done O(n) e.g. by keeping a variable with the position. This also avoid the problem of searching twice which is unnecessary expensive. You should also consider the behavior if there are two adjacent strings: should this produce an empty string in your sequence or should this be treated as if there were one space (in which case you might want to also use first_not_of()).

    Actually, I just reread the problem statement: if you just want to extract the string up to the first space, you don’t need erase(), empty(), or a loop if you insist on using assign(). However, this can be done using even two std::string members: find() and erase():

    line.erase(line.find(" \t\r\n\v\b\f"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to parse a document using SAX, and create a subdocument from
I would like to parse string literals using FParsec. By string literals I mean
I am querying data from http://www.imdbapi.com and would like to parse the result using
I would like to parse a string such as p1=6&p2=7&p3=8 into a NameValueCollection .
I would like to take a string representation of a set and parse it.
I would like to parse the values from aspx pages, both what is passed
I would like to know the procedure to adopt to parse and obtain text
I would like to know if there is a simple way to parse HTML
I would like to know if ParseExact is faster than Parse. I think that
When using double.Parse, it seems to like to string away any trailing (insignificant) zeros

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.