VC String Processing Collation

Source: Internet
Author: User
Tags strtok

Scene:

1. When storing data, interfaces sometimes need to combine string values and merge parts with special characters to split them when needed. such as some values, names and so on.

2.c++ has Strtok,stringstream and find functions to achieve segmentation. can be called depending on the situation.

#include <stdlib.h>#include<string.h>#include #include<iostream>#include<sstream>#include<vector>using namespacestd;voidTeststrtok () {//1. Non-thread-safe, if multiple threads are called at the same time, the original value will be overwritten. //2. Support for string segmentation. //3. The source string must be modifiable.    Charc_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); }} voidTestgetlinewithstringstream () {//1. Thread-safe, but only as a character delimiterStringStream SS ("google|twitter|facebook|microsoft|apple|ibm|"); stringstr;  while(Getline (SS,STR,'|') ) {cout<< Str <<Endl; }} voidTeststringfind () {//1. Implement it yourself, thread safe, support string as delimiter. The disadvantage may be that there is more code.    stringstr ="google| | twitter| | facebook| | microsoft| | apple| | ibm| |"; Const Char* Delim ="||"; Const intLen =strlen (Delim); size_t Index=0; size_t POS=Str.find (Delim,index);  while(POS! =string:: NPOs) {        stringSS = 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 () << en DL;    if((index+1) <str.length ()) {        stringSS = Str.substr (Index,str.length ()-index); cout<< SS <<Endl; }} intMainintargcChar Const*argv[]) {cout<<"Teststrtok:"<<Endl;    Teststrtok (); cout<<"Testgetlinewithstringstream:"<<Endl;    Testgetlinewithstringstream (); cout<<"Teststringfind:"<<Endl;     Teststringfind (); return 0;}

Output:

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 = "This is the first line \ n This is the second line \ n This is the third line \ n";

I want to use ' \ n ' to divide buf into three paragraphs and deposit them in a[1],a[2],a[3],
How do I do it ~

#include <stdio.h>#include<string.h>#include<malloc.h>intMain () {Char*a[3]; Char*buf ="This is the first line \ n This is the second line \ n This is the third line \ n"; Char*t, *pre =buf;inti =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]=' /'; ++i; Pre= T +1; }  for(i =0; I <3; ++i) {printf ("%s\n", A[i]);  Free(A[i]);} return 0;}

VC String Processing Collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.