string類簡介

來源:互聯網
上載者:User

標籤:使用   檔案   工作   ar   c++   line   size   js   

要使用 string 類,必須在標頭檔中包含頭文 string;

string 類位於名稱空間std;

使用string類時,某些操作比使用數組時更簡單;

如:不能將一個數組直接賦值給另一個數組;但可以將一個string對象直接賦值給另一個string對象;

例:string str1;

     string str2="panther";

     str1=str2;//直接賦值

     str1+=" paste";//直接可以相加

   int len1=str2.size();//panther paste長度13

而以前完成字串賦值等工作用標頭檔 cstring;

    #include <cstring>

    char charr1[20];

    char charr2[20]="jaguar";

有 函數

     strcpy(charr1,charr2);//複製

            strcat(charr1," juice");//追加

    int len2=strlen(charr1);//jaguar juice長度12

例:

  char charr[20];

  string str;

  cout<<strlne(charr);//隨機數值,遇到Null 字元停止

  cout<<str.size();//0

  cin.getline(charr,20);

     getline(cin,str);

其它形式的字串字面值

char  

wchar_t       //首碼 L

C++11:

char16_t    //首碼u

char32_t   //首碼U

例:

  wchar_t title[]=L"XXXXXXXXXX   XXXXXXXXX";

  char16_t name[]=u"XXXXXXXX    XXXXXX";

  char32_t car[]=U"XXXX    XXXX";

原始(raw)字串:字元表示自己;

  如\n不表示換行,而表示兩個常規字元:斜杠與n;

  將"(和")用作定界符,並用首碼R來標識原始字串;

     cout<<R"(;ljsdfl" T sdlfj "\n" ins of endl.)";//;ljsdfl" T sdlfj "\n" ins of endl.

  想輸出"(與“);

  cout<<R"+*("(who wouldn‘t?)",she whispered.)+*"<<endl;

  //輸出 "(who wouldn‘t?)",she whispered.

可混合使用如Ru,UR 等 ;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.