Selecting only the first few characters in a string C++

前端 未结 5 2001
遇见更好的自我
遇见更好的自我 2021-02-19 23:43

I want to select the first 8 characters of a string using C++. Right now I create a temporary string which is 8 characters long, and fill it with the first 8 characters of anot

5条回答
  •  我寻月下人不归
    2021-02-19 23:51

    If I have understood correctly you then just write

    std::string message = holder.substr( 0, 8 );
    

    Jf you need to grab characters from a character array then you can write for example

    const char *s = "Some string";
    
    std::string message( s, std::min( 8, std::strlen( s ) );
    

提交回复
热议问题