c语言spilt函数,c语言 split
C语言中字符切割函数split的实现
#include stdio.h
我们提供的服务有:成都网站建设、做网站、微信公众号开发、网站优化、网站认证、隆林ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的隆林网站制作公司
#include string.h
// 将str字符以spl分割,存于dst中,并返回子字符串数量
int split(char dst[][80], char* str, const char* spl)
{
int n = 0;
char *result = NULL;
result = strtok(str, spl);
while( result != NULL )
{
strcpy(dst[n++], result);
result = strtok(NULL, spl);
}
return n;
}
int main()
{
char str[] = "what is you name?";
char dst[10][80];
int cnt = split(dst, str, " ");
for (int i = 0; i cnt; i++)
puts(dst[i]);
return 0;
}
c中split的用法
c中split的用法你知道吗?下面我就跟你们详细介绍下c中split的用法,希望对你们有用。
c中split的用法如下:
1、用字符串分隔:
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";
string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
foreach (string i in sArray) Response.Write(i.ToString() + "br");
输出结果:
aaa
bbb
ccc
2、用多个字符来分隔:
string str="aaajbbbscccjdddseee";
string[] sArray=str.Split(new char[2] {'j','s'});
foreach(string i in sArray) Response.Write(i.ToString() + "br");
输出结果:
aaa
bbb
ccc
ddd
eee
3、用单个字符来分隔:
string str="aaajbbbjccc";
string[] sArray=str.Split('j');
foreach(string i in sArray) Response.Write(i.ToString() + "br");
输出结果:
aaa
bbb
ccc
////////////////////////////////////////////////
string[] arr = str.Split("o");
这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:
string str = "technology";
char[] separator = { 'o' };
string[] arr = str.Split(separator);
////////////////////////////////////////////////////
String.Split 方法有6个重载函数:
程序代码
1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1. public string[] Split(params char[] separator)
程序代码
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
2. public string[] Split(char[] separator, int count)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3. public string[] Split(char[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4. public string[] Split(string[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
需要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')
c语言自己编写一个split字符串分割函数
这个是用c++写的不过和c的原理也差不多我运行过了,有什么问题再问吧,c不知道能否运行,不过现在c/c++程序设计试验系统通常都有嵌套c++,你打开c界面时单击vc6.0创建一个项目运行就可以了。
#include iostream
using namespace std;
char** split(char p[],char c,int k)
{
int i,m,j;
char **q;
q=(char**)new char*[k];
for(i=0;ik;i++)
q[i]=(char*)new char*[20];
i=0;
j=0;
m=0;
while(p[i])
{
if(p[i]!=',')
{
q[j][m++]=p[i++];
}
else
{
q[j][m]='\0';
j++;
m=0;
i++;
}
}
q[k-1][m]='\0';
for(i=0;i4;i++)
{
coutq[i];
coutendl;
}
return q;
}
void main()
{ int k;
char p[]="ab,cde,efgh,ghi32";
char c=',';
int i=0,j=0,m=0;
while(p[i])
{
if(p[i]==',')
j++;
i++;
}
j=j+1;//接受到底有多少个单词在分割后通过逗号去判断
k=j;
char**q=split(p,c,k);
printf("\n");
for(i=0;i4;i++)
{
coutq[i];
coutendl;
}
}
本文名称:c语言spilt函数,c语言 split
网站地址:http://azwzsj.com/article/hcshod.html