字符串分割

9 篇文章 0 订阅

由于工作需要,自己找资料学习了,现在分享出来,有需要的同学看看。欢迎各位提出更好的办法,大家共同学习

先上C++版本的   Linux:g++

#include <iostream>
#include <sstream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
	string  str = "23,4 456,90 2,345 2,67";
	string sub;
	int x = 0;
	int y = 0;
	char ch_x[16] = {0};
	char ch_y[16] = {0};
	int len = 0;
	int len_1 = 0;
	int len_2 = 0;
	std::string::size_type pos = 0;

	istringstream iss(str);     //用于将字符串以空格分开,分割成子串
	cout << endl << "str = " << str << endl << endl;

	while(iss >> sub)
	{	
		cout << "sub is: " << sub <<endl;
		memset(ch_x, 0, sizeof(ch_x));
		memset(ch_y, 0, sizeof(ch_y));
		pos = sub.find(',');
		if (std::string::npos != pos)
		{
			len_1 = sizeof(ch_x) - 1;
			len_2 = pos;
			len = (len_1 > len_2) ? len_2 : len_1;
			if (len > 0)
			{
				strncpy(ch_x, sub.c_str(), len);
				ch_x[len + 1] = '\0';
			}

			len_1 = sizeof(ch_y) - 1;
			len_2 = sub.length() - (pos + 1);
			len = (len_1 > len_2) ? len_2 : len_1;
			if (len > 0)
			{
				strncpy(ch_y, sub.c_str() + (pos + 1) , len);
				ch_y[len + 1] = '\0';
			}
			x = atoi(ch_x);
			y = atoi(ch_y);
			cout <<"x = " << x << "  " << "y = " << y << endl << endl;
		}				
	}

	return 0;
}




#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{

	int i = 0;
	int x = 0;
	int y = 0;
	string strTemp = "12,34 56,789 3,2345 432,1";
	string str1;
	string str2;
	std::string::size_type pos = 0;
	string::size_type pos1 = 0;	
	int len = 0;
	int len_1 = 0;
	int len_2 = 0;
	char ch_x[16] = {0};
	char ch_y[16] = {0};
	for(i = 0; i < 4; i++)
	{
		pos1 = strTemp.find(' ');
		if(-1 == pos1) //没找到
		{
			str1 = str2;
		}
		else
		{
			str1 = strTemp.substr(0, pos1);  //空格之前的子串
			str2 = strTemp.substr(pos1 + 1, strTemp.length() - (pos1 + 1));  //空格之前的子串
		}
		memset(ch_x, 0, sizeof(ch_x));
		memset(ch_y, 0, sizeof(ch_y));
		pos = str1.find(',');
		if (std::string::npos != pos)
		{
			len_1 = sizeof(ch_x) - 1;
			len_2 = pos;
			len = (len_1 > len_2) ? len_2 : len_1;
			if (len > 0)
			{
				strncpy(ch_x, str1.c_str(), len);
				ch_x[len + 1] = '\0';
			}
		
			len_1 = sizeof(ch_y) - 1;
			len_2 = str1.length() - (pos + 1);
			len = (len_1 > len_2) ? len_2 : len_1;
			if (len > 0)
			{
				strncpy(ch_y, str1.c_str() + (pos + 1) , len);
				ch_y[len + 1] = '\0';
			}
			x = atoi(ch_x);
			y = atoi(ch_y);
			strTemp = str2;
		}
		cout << x << "," << y << endl;
		strTemp = str2;
		if(-1 == pos1)
		{
			break;
		}		
	}
	return 0;
}


#include <iostream>
#include <sstream>
#include <string.h>

using namespace std;

int main()
{
	string  str = "23,4 456,90 2,345 2,67";
	string sub;
	string s;
	
	istringstream iss(str);     //用于将字符串以空格分开,分割成字串
	cout << endl << "str = " << str << endl << endl;

	while(iss >> sub)
	{
		
		cout << "sub is: " << sub <<endl;
		istringstream issx(sub);
		
		/*getline()函数会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束。
		  1)到文件结束,2)遇到函数的定界符,3)输入达到最大限度   此处为情况二,界定符为',' 情况一导致while结束
		*/
		while(getline(issx, s, ','))   
		{
			cout << s << endl;
		}
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <sstream>

using namespace std;

int main() 
{ 
	string input = "mallid=31010\rtermno=139654\norder=rghrt gcode=x,y,z\npaydate=20051031\ncardno=52480365\rlimit=0804\nprice=@#$%"; 
	istringstream iss(input); //好多人说\n是默认的分界符,可是以上貌似空格、回车、换行都行
	copy( istream_iterator<string>(iss) , istream_iterator<string>(), ostream_iterator<string>(cout,"\n") ); 
	return 0; 
}


#if 0

int main()
{
	string o_str = "sadf x,y #<># *&%^";
	vector <string> str_list; 
	int comma_n = 0;
	int i = 4;
	do
	{
		string tmp_s = "";
		comma_n = o_str.find( " " );
		if( -1 == comma_n )
		{
			tmp_s = o_str.substr( 0, o_str.length() );
			str_list.push_back( tmp_s );
			break;
		}
		tmp_s = o_str.substr( 0, comma_n );
		o_str.erase( 0, comma_n+1 );  //返回一个迭代器,指向被删除的最后一个字符的下一个位置
		str_list.push_back( tmp_s );
		
	}while(i--);
	copy( str_list.begin(), str_list.end(), ostream_iterator<string>(cout,"\n") );
	return 0;
}
#endif





C版本 Linux:gcc   需要哪一段,放开#if 0即可

#include <stdio.h>  
#include <string.h>  

int main()
{
    char buf[] = "23,45 565,34 324,1 24,780";
    char *delim = ", ";
    char* token = strtok(buf, delim);
    while( token != NULL )
    {
        printf("%s ", token);
        token = strtok(NULL, delim);
    }
    printf("\n");
    return 0;
}




#if 0
int main()
{  
    char *source = "hello, world!, welcome to china!";  
    char *delim = " ,!";  //以该字符串中的任一字符分割
    char *token;
 
    /*功 能: 将串拷贝到新建的位置处
    strdup()在内部调用了malloc()为变量分配内存,不需要使用返回的字符串时,
    需要用free()释放相应的内存空间,否则会造成内存泄漏。
	这里由于后面strsep()的使用,free()只能释放一部分内存,故不加free(),
	这里只是为了学习strdup()
    */  
    char *s = strdup(source); 

    /* strsep()功能:分解字符串为一组字符串。从s指向的位置起向后扫描,
    遇到delim指向的字符串中的字符后,将此字符替换为NULL,然后将s更新
    指向到NULL的下一个字符串,返回s原来指向的地址
    */
    for(token = strsep(&s, delim); token != NULL; token = strsep(&s, delim)) 	 
    {  
        printf("%s",token);  
        printf("-"); 
    }  
    printf("\n");  
    return 0;  
} 
#endif



#if 0   
int main()
{
   char token[] ="12,34 1,456 23,5 146,34";  //定义成 *token 会发生段错误
   char *str = token;
   char *buf;
   while(str)
   {	
	buf = strsep(&str," ");
	printf("%s\n",buf);
   }
   return 0;
} 
#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值