需求:

数据库取出的字段类似于 "1,3,4"

然后用数字处理后,,比如 "1,2,3" 再存回去

#include<stdio.h>
#include<string.h> class MyStr{
public:
//根据 "1,2,3" 获得数字数组
static int *split(char * str,const char *split,const int count){
if(strlen(str)== || strlen(split)==)
return NULL;
if(count <=) return NULL;
int * ints=new int[count];
memset(ints,0x0,count*sizeof(int)); char split_str[];
int index_length=; snprintf(split_str,,"%%d%s",split);
for (int i=;i<count;i++){
sscanf(str+index_length,split_str,&ints[i]);
char num_str[]={};
sprintf(num_str,"%d",ints[i]);
index_length+=strlen(num_str)+strlen(split);
}
return ints;
}
//根据数字数组 组合为字符串
static char * bindNumbersToStr(int * nums,const unsigned int nums_length,const char * split){
char * str=new char[];
char num_str[]={};
int index_length=;
for(int i=;i<nums_length;i++){
sprintf(str+index_length,"%d%s",nums[i],split);
index_length=strlen(str);
}
str[index_length-strlen(split)]=0x0;
return str;
}
}; int main(){
char str[]="1,3,5,7,9";
int * ints=MyStr::split(str,",",);
for(int i=;i<;i++){
printf("ints[%d]=%d\n",i,ints[i]);
} char * s=MyStr::bindNumbersToStr(ints,,";");
printf("s=%s\n",s);
delete s;
delete ints; return ;
}

运行结果:

ints[0]=1
ints[1]=3
ints[2]=5
ints[3]=7
ints[4]=9
s=1;3;5;7;9

如预期所料

当然还可以进一步完善。

百度下,别人的方案:

http://zhidao.baidu.com/question/348273815.html

http://hi.baidu.com/hwygy_001/item/a073ff0d3eb743e4fe240d3b

http://www.cnblogs.com/huashanlin/archive/2011/04/25/2028597.html

C++对带有分隔符的字符串 分割为数字的通用解决方案的更多相关文章

  1. oracle根据分隔符将字符串分割成数组函数

    --创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...

  2. Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割

    ylbtech-Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割 1.返回顶部 1. Java 实例 - 字符串分割  Java 实例 以下实例使用了 split ...

  3. 从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )

    首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...

  4. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

  5. Oracle 超长字符串分割劈分

    Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...

  6. 工作中用到的oracle字符串分割整理

    oracle部分: 定义类型(用于字符串分割): create or replace TYPE "STR_SPLIT" IS TABLE OF VARCHAR2 (4000); 字 ...

  7. 【转】字符串分割(C++)

    原文:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/mysplitfuncpp.html 经常碰到字符串分割的问题,这里总结下,也方便我以后使 ...

  8. mysql字符串分割函数(行转列)

    由于工作需要需要处理一些以逗号分隔的字符串,每次都要现做很是麻烦,网上找了很多都没有现成的,好吧,自己动手写一个好了 )) ) BEGIN /*函数功能: 把带逗号的字符串分割取出 参数: num 要 ...

  9. SQL点滴3—一个简单的字符串分割函数

    原文:SQL点滴3-一个简单的字符串分割函数 偶然在电脑里看到以前保存的这个函数,是将一个单独字符串切分成一组字符串,这里分隔符是英文逗号“,”  遇到其他情况只要稍加修改就好了 CREATE FUN ...

随机推荐

  1. 不同浏览器的userAgent

    一.IE浏览器 //IE6 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" //IE7 "Mozill ...

  2. Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge

    Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, com ...

  3. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  4. D3D9 effect (hlsl)(转)

      转:http://blog.csdn.net/leonwei/article/details/8212800 effect其实整合了shader和render state的控制两大部分内容 9.1 ...

  5. Hadoop: the definitive guide 第三版 拾遗 第四章

    第四章中提到了通过CompressionCodec对streams进行压缩和解压缩,并提供了示例程序: 输入:标准输入流 输出:压缩后的标准输出流 // cc StreamCompressor A p ...

  6. WiX: uninstall older version of the application

    I have installer generated by WiX and I want it to ask: "You have already installed this app. D ...

  7. ASP.NET MVC中实现属性和属性值的组合,即笛卡尔乘积02, 在界面实现

    在"ASP.NET MVC中实现属性和属性值的组合,即笛卡尔乘积01, 在控制台实现"中,在控制台应用程序中实现了属性值的笛卡尔乘积.本篇在界面中实现.需要实现的大致如下: 在界面 ...

  8. 再议ASP.NET MVC中CheckBoxList的验证

    在ASP.NET MVC 4中谈到CheckBoxList,经常是与CheckBoxList的显示以及验证有关.我在"MVC扩展生成CheckBoxList并水平排列"中通过扩展H ...

  9. Net Framework 2.0 MSI returned error code 1603解决方法

    出现这种情况的原因,主要是用ghost做的系统,有很多系统中把ie给绑架了.下面的截图就是ghost做的系统中注册表的显示,通过上面的方法就可以解决这种Microsoft .NET Framework ...

  10. 【Android病毒分析报告】- 手机支付毒王“银行悍匪”的前世今生

    from://http://blog.csdn.net/androidsecurity/article/details/18984165 2014年1月8日,央视曝光了一款名为“银行悍匪”的手机银行木 ...