开发者

How can I assign pointer member with long string?

When I did the practice below to erase my pointer member and assign new value to it.

(*pMyPointer).member.erase();
(*pMyPointer).member.assign("Hello"); // Successfully

Than I tried more...

(*pMyPointer).member.erase();
(*pMyPointer).member.assign("Long Multi Lines Format String"); // How to?

If the long multi lines string ca开发者_Python百科n't quote by double quoter, how to handle it. Thank you.


I really have no clue what you are trying to ask. Maybe this:

(*pMyPointer).member.assign("Long Multi Lines Format String"
                            "more lines that will be"
                            "concatenated by the compiler");

Or did you mean line breaks like this:

(*pMyPointer).member.assign("Long Multi Lines Format String\n"
                            "more lines that will be\n"
                            "concatenated by the compiler");


Line breaks in string literals are '\n':

"This is a string literal\nwith a line break in it."


I assume you mean passing a very long string constant as a parameter, in which case C++ does the string-merging for you: printf("hello, " "world"); is the same thing as printf("hello, world");

Thus:

(*pMyPointer).member.assign("Long Multi Lines Format String "
       "and here's more to the string "
       "and here's more to the string "
       "and here's more to the string "
       "and here's more to the string "
       "and here's more to the string ");


I think the question is is how to create a multi-line string.

You can easily do it with:

(*pMyPointer).member.assign(
    "Long Multi Lines Format String" \
    "Long Multi Lines Format String" \
    "Long Multi Lines Format String"
 );

You'll have to add a \n to the string if you want to return. Otherwise it's going to stay on the same line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜