zoukankan      html  css  js  c++  java
  • VC字符串处理整理

    场景:

    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:
    google
    twitter
    facebook
    microsoft
    apple
    ibm
    TestGetLineWithStringStream:
    google
    twitter
    facebook
    microsoft
    apple
    ibm
    TestStringFind:
    google
    twitter
    facebook
    microsoft
    apple
    ibm
    [Finished in 0.2s]
     

     

    char* a[3];
    char* buf ="这是第一行 这是第二行 这是第三行 ";

    我想要用' '符将buf分割成三段并分别存入a[1],a[2],a[3]中,
    请问该怎么做~

    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    int main()
    {
     char *a[3];
     char *buf ="这是第一行
    这是第二行
    这是第三行
    "; 
     char *t, *pre = buf;
     int i = 0, l;
    
     while (t = strchr(pre, '
    '))
     {
      if (i >= 3)
       break;
    
      l = t - pre;
      a[i] = (char *)malloc(l + 1);
      strncpy(a[i], pre, l);
      a[i][l] = '';
      ++i;
      pre = t + 1;
     }
    
     for (i = 0; i < 3; ++i)
     {
      printf("%s
    ", a[i]);
      free(a[i]);
     }
    
     return 0;
    }
  • 相关阅读:
    C博客01——分支,顺序结构
    博客园,我昔日的精神家园
    201720182 20179207 《网络攻防技术》第二周作业
    python 面试题(2)
    python 面试题(1)
    201720182 20179207 《网络攻防技术》第一周作业
    201720182 20179207 《网络攻防技术》第三周作业
    201720182 20179207 《网络攻防技术》黑客与工具
    python socket和简单tcp通信实现
    201720182 20179207 《网络攻防技术》python简明教程(110)
  • 原文地址:https://www.cnblogs.com/x-poior/p/5034266.html
Copyright © 2011-2022 走看看