VC字串處理整理

來源:互聯網
上載者:User

標籤:

情境:

1.在儲存資料時有時介面需要合并字串值,並以某些特殊字元來合并部分,到需要的時候再分割它。如一些數值,人名等。

2.C++有strtok,stringstream和find函數來實現分割。可以根據情況調用。

 

#include <stdlib.h>#include <string.h>#include #include <iostream>#include <sstream>#include <vector>using namespace std; void TestStrtok(){    //1.非安全執行緒的,如果多個線程同時調用,會覆蓋掉原來的值.    //2.支援以字串分割.    //3.源字串必須是可修改的.    char c_str[]="google||twitter||facebook||microsoft||apple||ibm||";    const char* delim = "||";    char* result = strtok(c_str,delim);    while(result != NULL)    {        cout << result << endl;        result = strtok(NULL,delim);    }} void TestGetLineWithStringStream(){    //1.安全執行緒的,但是只能以字元作為分隔字元    stringstream ss("google|twitter|facebook|microsoft|apple|ibm|");    string str;    while(getline(ss,str,‘|‘))    {        cout << str << endl;    }} void TestStringFind(){    //1.自己實現,安全執行緒,支援字串作為分隔字元.缺點可能就是代碼量多.    string str = "google||twitter||facebook||microsoft||apple||ibm||";    const char* delim = "||";    const int len = strlen(delim);    size_t index = 0;    size_t pos = str.find(delim,index);    while(pos != string::npos)    {        string ss = str.substr(index,pos-index);        cout << ss << endl;        index = pos+len;        pos = str.find(delim,index);    }     //cout << "is last?" << " index:" << index << " str.length():" << str.length() << endl;    if((index+1) < str.length())    {        string ss = str.substr(index,str.length() - index);        cout << ss << endl;    }} int main(int argc, char const *argv[]){    cout << "TestStrtok: " << endl;    TestStrtok();    cout << "TestGetLineWithStringStream: " << endl;    TestGetLineWithStringStream();    cout << "TestStringFind: " << endl;    TestStringFind();     return 0;}

 

 

輸出:

TestStrtok: googletwitterfacebookmicrosoftappleibmTestGetLineWithStringStream: googletwitterfacebookmicrosoftappleibmTestStringFind: googletwitterfacebookmicrosoftappleibm[Finished in 0.2s]  
char* a[3];
char* buf ="這是第一行\n這是第二行\n這是第三行\n";

我想要用‘\n‘符將buf分割成三段並分別存入a[1],a[2],a[3]中,
請問該怎麼做~

#include <stdio.h>#include <string.h>#include <malloc.h>int main(){ char *a[3]; char *buf ="這是第一行\n這是第二行\n這是第三行\n";  char *t, *pre = buf; int i = 0, l; while (t = strchr(pre, ‘\n‘)) {  if (i >= 3)   break;  l = t - pre;  a[i] = (char *)malloc(l + 1);  strncpy(a[i], pre, l);  a[i][l] = ‘\0‘;  ++i;  pre = t + 1; } for (i = 0; i < 3; ++i) {  printf("%s\n", a[i]);  free(a[i]); } return 0;}

 

 

 

VC字串處理整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.