C语言中的字符串中的分隔---split
2014-01-03 21:55
330 查看
这个方法中运用到了strtok函数:
原型:
char *strtok(char s[], const char *delim);
功能:
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。
使用中的注意:
strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果
要保持原字符串的完整,可以使用strchr和sscanf的组合等
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int strsplinum(char *str, const char*del) //判断总共有多少个分隔符,目的是在main函数中构造相应的arr指针数组
{
char *first = NULL;
char *second = NULL;
int num = 0;
first = strstr(str,del);
while(first != NULL)
{
second = first+1;
num++;
first = strstr(second,del);
}
return num;
}
void split( char **arr, char *str, const char *del)//字符分割函数的简单定义和实现
{
char *s =NULL;
s=strtok(str,del);
//printf("s:%s\n",s);
while(s != NULL)
{
//printf("s:%s\n",s);
*arr++ = s;
//printf("%s\n",*arr);
s = strtok(NULL,del);
}
}
int main(int argc,char *argv)
{
char *mm="2014-1-3-16-34-213";
char str[16] ;
strcpy(str,mm);
const char *del = "-"; //分隔符
int numTest = 1;
int i=0;
char *arr[6]; //利用指针数组对应分割好的字符串数组(即二维数组中的行数组)
numTest = strsplinum(str,del);
printf("the numbers of arry is : %d \n",numTest+1);
split(arr,str,del);
while(i<=numTest)
{
printf("%s\n",*(arr+(i++))); //打印分割好的字符串
}
return 0;
}
结果:
the numbers of arry is : 6
2014
1
3
16
34
213
请按任意键继续. . .
方法二(C++方法):
#include <string.h>
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main(int argc,char **argv)
{
//string test = "aa aa bbc cccd";
string test="2014-1-3-2";
vector<string> strvec;
string strtemp;
string::size_type pos1, pos2;
pos2 = test.find('-');
pos1 = 0;
while (string::npos != pos2)
{
strvec.push_back(test.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = test.find('-', pos1);
}
strvec.push_back(test.substr(pos1));
vector<string>::iterator iter1 = strvec.begin(), iter2 = strvec.end();
char *m;
while (iter1 != iter2)
{
cout << *iter1 << endl;
++iter1;
}
//cout<<m<<endl;
return 0;
}
结果:
2014
1
3
2
请按任意键继续. . .
原型:
char *strtok(char s[], const char *delim);
功能:
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。
使用中的注意:
strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果
要保持原字符串的完整,可以使用strchr和sscanf的组合等
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int strsplinum(char *str, const char*del) //判断总共有多少个分隔符,目的是在main函数中构造相应的arr指针数组
{
char *first = NULL;
char *second = NULL;
int num = 0;
first = strstr(str,del);
while(first != NULL)
{
second = first+1;
num++;
first = strstr(second,del);
}
return num;
}
void split( char **arr, char *str, const char *del)//字符分割函数的简单定义和实现
{
char *s =NULL;
s=strtok(str,del);
//printf("s:%s\n",s);
while(s != NULL)
{
//printf("s:%s\n",s);
*arr++ = s;
//printf("%s\n",*arr);
s = strtok(NULL,del);
}
}
int main(int argc,char *argv)
{
char *mm="2014-1-3-16-34-213";
char str[16] ;
strcpy(str,mm);
const char *del = "-"; //分隔符
int numTest = 1;
int i=0;
char *arr[6]; //利用指针数组对应分割好的字符串数组(即二维数组中的行数组)
numTest = strsplinum(str,del);
printf("the numbers of arry is : %d \n",numTest+1);
split(arr,str,del);
while(i<=numTest)
{
printf("%s\n",*(arr+(i++))); //打印分割好的字符串
}
return 0;
}
结果:
the numbers of arry is : 6
2014
1
3
16
34
213
请按任意键继续. . .
方法二(C++方法):
#include <string.h>
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main(int argc,char **argv)
{
//string test = "aa aa bbc cccd";
string test="2014-1-3-2";
vector<string> strvec;
string strtemp;
string::size_type pos1, pos2;
pos2 = test.find('-');
pos1 = 0;
while (string::npos != pos2)
{
strvec.push_back(test.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = test.find('-', pos1);
}
strvec.push_back(test.substr(pos1));
vector<string>::iterator iter1 = strvec.begin(), iter2 = strvec.end();
char *m;
while (iter1 != iter2)
{
cout << *iter1 << endl;
++iter1;
}
//cout<<m<<endl;
return 0;
}
结果:
2014
1
3
2
请按任意键继续. . .
相关文章推荐
- C#中Split分隔字符串的应用
- asp.net下Split分隔字符串的应用
- Python使用split使用多个字符分隔字符串
- SQL自定义函数split分隔字符串
- Split a String(分隔字符串)
- aspx页面中用Split分隔字符串
- asp.net下Split分隔字符串的应用
- asp.net下Split分隔字符串的应用
- split 分隔字符串
- 字符串的分隔方法 split()
- Oracle使用split和splitstr函数批量分隔字符串
- python split分隔字符串之分隔次数
- C语言编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
- C#中Split分隔字符串的应用(C#、split、分隔、字符串)
- asp.net下Split分隔字符串的应用
- 一个扩展后的Split方法,可根据字符串分隔字符,返回字符数组
- oracle 拆分逗号分隔字符串 实现split
- oracle 存储过程 字符串分隔 FUNC_GETSPLITSTR
- 在JAVA中, 在使用String.split方法时要注意的一些特殊分隔字符串
- C#中Split用法~用字符串分隔,截取...