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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:21:10+00:00 2026-05-17T16:21:10+00:00

Not to be confused with how to split a string parsing wise, e.g.: Split

  • 0

Not to be confused with how to split a string parsing wise, e.g.:
Split a string in C++?

I am a bit confused as to how to split a string onto multiple lines in c++.

This sounds like a simple question, but take the following example:

#include <iostream>
#include <string>
main() {
  //Gives error
  std::string my_val ="Hello world, this is an overly long string to have" +
    " on just one line";
  std::cout << "My Val is : " << my_val << std::endl;

  //Gives error
  std::string my_val ="Hello world, this is an overly long string to have" &
    " on just one line";  
  std::cout << "My Val is : " << my_val << std::endl;
}

I realize that I could use the std::string append() method, but I was wondering if there was any shorter/more elegant (e.g. more pythonlike, though obviously triple quotes etc. aren’t supported in c++) way to break strings in c++ onto multiple lines for sake of readability.

One place where this would be particularly desirable is when you’re passing long string literals to a function (for example a sentence).

  • 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-17T16:21:10+00:00Added an answer on May 17, 2026 at 4:21 pm

    Don’t put anything between the strings. Part of the C++ lexing stage is to combine adjacent string literals (even over newlines and comments) into a single literal.

    #include <iostream>
    #include <string>
    main() {
      std::string my_val ="Hello world, this is an overly long string to have" 
        " on just one line";
      std::cout << "My Val is : " << my_val << std::endl;
    }
    

    Note that if you want a newline in the literal, you will have to add that yourself:

    #include <iostream>
    #include <string>
    main() {
      std::string my_val ="This string gets displayed over\n" 
        "two lines when sent to cout.";
      std::cout << "My Val is : " << my_val << std::endl;
    }
    

    If you are wanting to mix a #defined integer constant into the literal, you’ll have to use some macros:

    #include <iostream>
    using namespace std;
    
    #define TWO 2
    #define XSTRINGIFY(s) #s
    #define STRINGIFY(s) XSTRINGIFY(s)
    
    int main(int argc, char* argv[])
    {
        std::cout << "abc"   // Outputs "abc2DEF"
            STRINGIFY(TWO)
            "DEF" << endl;
        std::cout << "abc"   // Outputs "abcTWODEF"
            XSTRINGIFY(TWO) 
            "DEF" << endl;
    }
    

    There’s some weirdness in there due to the way the stringify processor operator works, so you need two levels of macro to get the actual value of TWO to be made into a string literal.

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

Sidebar

Related Questions

This is not to be confused with How to tell if a DOM element
One thing that's always confused me is input escaping and whether or not you're
Not sure if this is possible or if I'm expressing correctly what I'm looking
Not that I'm doing something like that, but I am kind of interested how
Not sure how to ask a followup on SO, but this is in reference
Not too practical maybe, but still interesting. Having some abstract question on matrix multiplication
Not very technical, but... I have to implement a bad words filter in a
(Not related to versioning the database schema) Applications that interfaces with databases often have
Not really getting the point of the map function. Can anyone explain with examples
Not too long ago, I had a problem which required me to set WinDbg.exe

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.