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 ...
随机推荐
- Python3 贝叶斯分类
# -*- coding: utf-8 -*- """ Created on Tue Jan 16 20:11:07 2018 @author: markli " ...
- queue模块回顾
queue queue是python中的标准库,俗称队列. 在python中,多个线程之间的数据是共享的,多个线程进行数据交换的时候,不能够保证数据的安全性和一致性,所以当多个线程需要进行数据交换的时 ...
- "Unchecked-Send"漏洞分析
author:sf197tl;dr国内并没有一个文档有讲述该漏洞的,正好闲着没事.就写下这篇文章.在网上也搜寻了一些资料,通过自己的翻译才有今天的这篇文章.该漏洞在DASP TOP 10中可以查看到. ...
- Java内存泄露分析和解决方案及Windows自带查看工具
Java内存泄漏是每个Java程序员都会遇到的问题,程序在本地运行一切正常,可是布署到远端就会出现内存无限制的增长,最后系统瘫痪,那么如何最快最好的检测程序的稳定性,防止系统崩盘,作者用自已的亲身经历 ...
- Xtreme8.0 - Sum it up 水题
Sum it up 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/sum-it-up Descr ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem L. Stock Trading Robot 水题
Problem L. Stock Trading Robot 题目连接: http://www.codeforces.com/gym/100253 Description CyberTrader is ...
- QThreadPool线程池的开发使用
QThreadPool+QRunnable线程池与QThread线程两种方式使用的场景不同,QThreadPool+QRunnable线程池主要用于那种不需要一直运行的任务,而QThread主要用于长 ...
- 马士兵hadoop第五课:java开发Map/Reduce
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- MyBatis中使用#{}和${}的区别
select * from table_name where id=#{id}; select * from table_name where id=${id}; 区别: 在动态SQL解析阶段,#{} ...
- 用.Net如何访问Linux下目录
很多Windows下的应用需要访问和监控Linux下的目录,本文便介绍如何实现. 只需要搭建配置samba服务,即可将Linux下的目录变得如同Windows下共享可写. 1.服务查询 默认情况下,L ...