What is it called when you can fill a string with <<< and an end-delimiter?

后端 未结 4 1668
忘了有多久
忘了有多久 2021-01-19 19:51

I know in C++ and in PHP you can fill a string or a file with hard-coded text. If I remember correctly this is how it is supposed to look:

var <<< D         


        
相关标签:
4条回答
  • 2021-01-19 20:08

    Multiline string literal?

    0 讨论(0)
  • 2021-01-19 20:11

    It's called the HEREDOC syntax in PHP:

    <?php
    
    $str = <<<EOD
    Example of string
    spanning multiple lines
    using heredoc syntax.
    EOD;
    
    ?>
    
    0 讨论(0)
  • 2021-01-19 20:11

    C++ doesn't have any equivalent to PHP's HEREDOC syntax.

    You can, however, do this in C++:

    cout << "   Menu for program X\n"
            "   1.Add two numbers\n"
            "   2.Substract two numbers\n"
            "   3.Multiply two numbers\n"
            "   Please pick an option from (0-3);" << endl;
    

    Or this in C:

    printf( "   Menu for program X\n"
            "   1.Add two numbers\n"
            "   2.Substract two numbers\n"
            "   3.Multiply two numbers\n"
            "   Please pick an option from (0-3);\n" );
    fflush(stdout);
    

    Which is directly equivalent to PHP's HEREDOC syntax:

    echo <<<EOT
       Menu for program X
       1.Add two numbers
       2.Substract two numbers
       3.Multiply two numbers
       Please pick an option from (0-3);
    EOT;
    

    The above syntax for C and C++ is treated by the compiler as one long string, by stitching them together. It has no other effect on the string literal, hence the need for '\n'.

    0 讨论(0)
  • 2021-01-19 20:31

    In Windows you store long text as a resource or as a file.

    You can store it in a Unixish by using internationalization (I18) libraries. It's a good hack to write the string in your program like printf( _("_xmenu_ %d, %d, %d"), 1, 2, 3) and then you can "translate" _xmenu_ into English, as well as whatever other languages you want to support. Because the translations are stored in another file, you don't have to look at them all the time and they're easy to change without recompiling.

    0 讨论(0)
提交回复
热议问题