C++:实现split分割字符串
来源:互联网 发布:如何修改路由器mac地址 编辑:程序博客网 时间:2024/04/27 03:20
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void split(const string& src, const string& separator, vector<string>& dest)
{
string str = src;
string substring;
string::size_type start = 0, index;
do
{
index = str.find_first_of(separator,start);
if (index != string::npos)
{
substring = str.substr(start,index-start);
dest.push_back(substring);
start = str.find_first_not_of(separator,index);
if (start == string::npos) return;
}
}while(index != string::npos);
//the last token
substring = str.substr(start);
dest.push_back(substring);
}
int main()
{
string src = "Accsvr:tcp -h 127.0.0.1 -p \t 20018 ";
vector<string> d, s;
vector<string>::iterator p, q;
split(src,":",d);
for(p=d.begin();p!=d.end();++p)
{
cout << *p << endl;
s.clear();
split(*p," \t\n",s);
for (q=s.begin();q!=s.end();++q)
cout << "\t" << *q << endl;
}
return 0;
}
C语言实现方法一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void split(char *src, const char *separator, char **dest, int *num)
{
char *pNext;
int count = 0;
if (src == NULL || strlen(src) == 0) return;
if (separator == NULL || strlen(separator) == 0) return;
pNext = strtok(src,separator);
while(pNext != NULL)
{
*dest++ = pNext;
++count;
pNext = strtok(NULL,separator);
}
*num = count;
}
int main()
{
char src[] = "Accsvr:tcp -h 127.0.0.1 -p\n 20018";
char *dest[128];
char *dest2[128];
int num = 0, num2 = 0;
int i, j;
split(src,":",dest,&num);
for (i=0;i<num;++i)
{
printf("|%s|\n",dest[i]);
split(dest[i]," \t\n",dest2,&num2);
for (j=0;j<num2;++j)
{
printf("|%s|\n",dest2[j]);
}
}
return 0;
}
C语言实现方法二:功能与方法一有区别
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void split(char *src, const char *separator, char **dest, int *num)
{
char *pSeparator, *pStart, *pEnd;
unsigned int sep_len;
int count = 0;
if (src == NULL || strlen(src) == 0) return;
pSeparator = (char *)malloc(16);
if (pSeparator == NULL) return;
if (separator == NULL || strlen(separator) == 0) strcpy(pSeparator," ");/* one blank by default */
else strcpy(pSeparator,separator);
sep_len = strlen(pSeparator);
pStart = src;
while(1)
{
pEnd = strstr(pStart, pSeparator);
if (pEnd != NULL)
{
memset(pEnd,'\0',sep_len);
*dest++ = pStart;
pEnd = pEnd + sep_len;
pStart = pEnd;
++count;
}
else
{
*dest = pStart;
++count;
break;
}
}
*num = count;
if (pSeparator != NULL) free(pSeparator);
}
int main()
{
char src[] = "Accsvr:tcp -h 127.0.0.1 -p 20018";
char *dest[128];
char *dest2[128];
int num = 0, num2 = 0;
int i, j;
split(src,":",dest,&num);
for (i=0;i<num;++i)
{
printf("|%s|\n",dest[i]);
split(dest[i],"\t",dest2,&num2);
for (j=0;j<num2;++j)
{
printf("|%s|\n",dest2[j]);
}
}
return 0;
}
来源:http://www.rosoo.net/a/201106/14519.html
- C++:实现split分割字符串
- C++:实现split分割字符串
- C++:实现split分割字符串
- C++:实现split分割字符串
- C++:实现split分割字符串
- C++:实现split分割字符串
- C/C++实现split分割字符串
- C/C++实现split分割字符串
- C++:实现split分割字符串 - 改进
- 分割字符串函数split(C#)
- 分割字符串函数split(C#)
- c 分割字符串函数 split
- C++ split 字符串分割实现
- ORACLE实现字符串分割SPLIT
- 用C/C++实现Java中的split函数分割字符串
- 用Matlab实现字符串分割(split)
- oracle实现字符串分割函数 split()函数
- lua字符串分割 string.split实现
- 一桐对第三方SEO博客的一点拙见
- iphone----NSMutableSet
- Extjs框架viewport总结之最简单的viewport案例图解(入门专用)
- php 判断是否为移动设备访问
- 论文排版:Word加入脚注后,分栏的正文就跑到下一页上去了的解决方案
- C++:实现split分割字符串
- 使用wvdial启动ppp协议拨号上网
- android屏幕适配不同的资源图片、布局
- C++的四种cast操作符的区别--类型转换
- Tomcat 配置Manager用户名与密码
- 怎么会有这样的代码
- 使用Spinner和setDropDownViewResource
- 更改android锁屏方向
- 一个shell文件的分析记录