C++对带有分隔符的字符串 分割为数字的通用解决方案
需求:
数据库取出的字段类似于 "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++对带有分隔符的字符串 分割为数字的通用解决方案的更多相关文章
- oracle根据分隔符将字符串分割成数组函数
--创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...
- Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割
ylbtech-Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割 1.返回顶部 1. Java 实例 - 字符串分割 Java 实例 以下实例使用了 split ...
- 从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )
首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...
- SQL Server 游标运用:鼠标轨迹字符串分割
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...
- Oracle 超长字符串分割劈分
Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...
- 工作中用到的oracle字符串分割整理
oracle部分: 定义类型(用于字符串分割): create or replace TYPE "STR_SPLIT" IS TABLE OF VARCHAR2 (4000); 字 ...
- 【转】字符串分割(C++)
原文:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/mysplitfuncpp.html 经常碰到字符串分割的问题,这里总结下,也方便我以后使 ...
- mysql字符串分割函数(行转列)
由于工作需要需要处理一些以逗号分隔的字符串,每次都要现做很是麻烦,网上找了很多都没有现成的,好吧,自己动手写一个好了 )) ) BEGIN /*函数功能: 把带逗号的字符串分割取出 参数: num 要 ...
- SQL点滴3—一个简单的字符串分割函数
原文:SQL点滴3-一个简单的字符串分割函数 偶然在电脑里看到以前保存的这个函数,是将一个单独字符串切分成一组字符串,这里分隔符是英文逗号“,” 遇到其他情况只要稍加修改就好了 CREATE FUN ...
随机推荐
- DFT,DTFT,DFS,FFT区别
学习了数字信号处理之后,被里面的几个名词搞的晕头转向,比如DFT,DTFT,DFS,FFT,FT,FS等,FT和FS属于信号与系统课程的内容,是对连续时间信号的处理,这里就不过多讨论,只解释一 ...
- Qt代码创建分隔条
我们在使用Qt时,会发现在使用UI界面时可以添加窗口分隔条,但我们使用纯代码时却没有分隔条的类函数.这时候我们就需要手动添加,添加方式和Qt自定义标签按钮一样,下面我直接写代码.如果不知道怎么添加的, ...
- SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms.
SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after ...
- liunx的命令大全
- CF1065E Side Transmutations
link 题意: 给你一个长为m的序列$b_i$,定义两个字符串a,b相同当前仅当a执行以下操作后能变成b:($\rm{prefix}(x,y)$及$\rm{suffix}(x,y)$定义为串x的前/ ...
- jersey练习
package com.tz.router; import java.util.ArrayList; import java.util.Date; import java.util.List; imp ...
- Spring boot整合jsp
这几天在集中学习Spring boot+Shiro框架,因为之前view层用jsp比较多,所以想在spring boot中配置jsp,但是spring boot官方不推荐使用jsp,因为jsp相对于一 ...
- NAS系统收集
FreeNAS®,目前最受欢迎的开源免费 NAS 操作系统之一,基于以安全和稳定著称的 FreeBSD 系统开发,由 ixsystems 公司的技术团队维护.项目地址:www.freenas.org ...
- JavaScript获取事件对象和目标对象
在JavaScript开发中,经常需要获取触发某个事件的目标对象.让后根据目标对象进行不同的业务处理.下面展示通过JavaScript获取触发事件的事件目标对象.如下: Js代码 1 2 3 4 5 ...
- ISO 7816-4: Annex A: Transportation of APDU messages by T=0
http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_annex-a.aspx Annex A: Transportation ...