Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a break line in string?

How can i put break line in string.
Something like this.

string var = "hey
s";

Would be something like this.

hey
s
like image 979
Ramilol Avatar asked Dec 25 '10 07:12

Ramilol


People also ask

How do you insert a line break character?

Place the cursor where you want the line break. Use the keyboard shortcut – ALT + ENTER (hold the ALT key and then press Enter).

How do you break a line in a string Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

How do you put a line break in a text file?

There is carriage return with the escape sequence \r with hexadecimal code value 0D abbreviated with CR and line-feed with the escape sequence \n with hexadecimal code value 0A abbreviated with LF. Text files on MS-DOS/Windows use CR+LF as newline. Text files on Unix/Linux/MAC (since OS X) use just LF as newline.


2 Answers

You should just put a \n between hey and s. So:

string var = "hey\ns";
like image 58
dshipper Avatar answered Sep 23 '22 18:09

dshipper


Line breaking can be achieved using Dan's advice:

string var = "hey\ns";

Note that you cannot do this the way you wanted:

string var = "hey     // this is not
s";                   // valid code

and it's a design choice of C++.

Older languages generally do not allow you to define multiline strings.

But, for example, Python does allow you exactly this:

someString = """
    this is a
    multiline
    string
"""

and printing someString will give you a true multiline string.

You can forget about this when using C++, though.

like image 39
darioo Avatar answered Sep 22 '22 18:09

darioo