纯C 实现 strpos substr strspilt str_trim
在C 语言中没有C++ 好用的 spilt 方法 (STL 带的也不怎么好用)
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; int Cstrpos(char *haystack, const char *needle)
{
char *p;
p = strstr(haystack, needle);
if(p)
{
return p - haystack;
}
return -;
} int Csubstr(char *haystack, int start, int len, char *out)
{
int i;
for(i=; i<len; i++)
{
out[i] = haystack[start+i];
}
return ;
} char *Csplit(char *haystack, const char *needle, char *out)
{
int start, end, offset, i;
//match start
if( == memcmp(haystack, needle, strlen(needle)))
{
start = Cstrpos(haystack, needle);
if(- == start)
{
return NULL;
}
}
else
{
start = ;
}
end = Cstrpos(haystack+start, needle);
if(- == end)
{
end = strlen(haystack) - start;
}
offset = end; for(i=; i<offset; i++)
{
out[i] = haystack[i];
}
haystack += i+; return haystack;
} int main(int argc, char **argv)
{
//char *str = "123,456,789"; //2个测试字符串
char *str = "";
cout<<str<<endl; int pos1;
pos1 = Cstrpos(str, ",");
cout<<pos1<<endl; char out[] = {}; //Csubstr(str, 1, 3, out);
//cout<<out<<endl; cout<<"+++++++++++++++++++++"<<endl; memset(out, , );
str = Csplit(str, ",", out);
cout<<out<<endl; cout<<"+++++++++++++++++++++"<<endl; memset(out, , );
str = Csplit(str, ",", out);
cout<<out<<endl; cout<<"+++++++++++++++++++++"<<endl; memset(out, , );
str = Csplit(str, ",", out);
cout<<out<<endl; cout<<"+++++++++++++++++++++"<<endl; memset(out, , );
str = Csplit(str, ",", out);
cout<<out<<endl; return ;
}
因为也包含了测试程序 使用 cout 输出,所以使用 g++ 编译,执行。但3个函数是 可以移值到 ARM 、 KEIL、STM32 中的。
测试123,456,789

测试123

这个,目前还有一个缺点,分隔符只支持1个字符。
补发一个,实现的 str_trim 函数 需要头文件 #include <ctype.h>
static void str_trim(char *str)
{
int len;
char *copy;
char *end, *start; len = strlen(str);
copy = (char *)malloc(len + ); if(! copy)
{
logd("malloc error \n");
return ;
} memset(copy, , len + );
strcpy(copy, str);
start = copy;
end = start + len - ; while(end >= start)
{
if(! isgraph(*end))
{
*end = '\0';
end--;
}
else
{
break;
}
} len = strlen(copy);
end = start + len - ;
while(start <= end)
{
if(! isgraph(*start))
{
start++;
}
else
{
break;
}
} strcpy(str, start);
free(copy);
}
纯C 实现 strpos substr strspilt str_trim的更多相关文章
- php strpos() 函数介绍与使用方法详解
本文主要和大家介绍PHP中mb_strpos的使用技巧,通过使用语法以及实例给大家详细分析了用法,需要的朋友参考学习下.希望能帮助到大家.mb_strpos(PHP 4 >= 4.0.6, PH ...
- php生成各种验证码
片段 1 片段 2 片段 3 index.html ```<script type="text/javascript" src="jquery.min.js&quo ...
- [译] 给PHP开发者的PHP源码-第一部分-源码结构
文章来自:http://www.hoohack.me/2016/02/04/phps-source-code-for-php-developers-ch 原文:http://blog.ircmaxel ...
- [php入门] 2、基础核心语法大纲
1 前言 最近在学PHP,上节主要总结了PHP开发环境搭建<[php入门] 1.从安装开发环境环境到(庄B)做个炫酷的登陆应用>.本节主要总结PHP的核心基础语法,基本以粗轮廓写,可以算作 ...
- PHP利用jquery生成各种验证码和Ajax验证
PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: .产生一张png的图片: .为图片设置背景 ...
- php 解析 视频 信息 封面 标题 图片 支持 优酷, 土豆 酷6 56 新浪 qq播客 乐视 乐视
原文地址:http://www.lianyue.org/2013/2497/ <?php /** * 解析 视频信息 类 * * 支持 优酷, 土豆 酷6 56 新浪 qq播客 乐视 乐视 ** ...
- 一个PHP写的简单webservice服务端+客户端
首先是服务端,服务端有一个主要的class组成:apiServer.php <?php /** * apiServer.php * * webservice主类 * * @filename ap ...
- Curl 采集乱码 gzip 原因及解决方案 utf-8
用curl获取一个经过gzip压缩后的网页时返回乱码 原因大体就是服务器返回的Content-Encoding的值和网页的编码不同,造成curl解码出问题,直接将gzip或deflate编码的文件下载 ...
- 【代码实现】PHP生成各种随机验证码
原文地址:http://www.phpthinking.com/archives/531 验证码在WEB应用中很重要,通经常使用来防止用户恶意提交表单,如恶意注冊和登录.论坛恶意灌水等.本文将通过实例 ...
随机推荐
- 三:mysql条件查询
1:查询工资等于5000的员工
- 年度Java技术盘点,懂这些技术的程序员2019发展大好
与一年前一样,Java仍然是最流行的编程语言.据TIOBE的数据显示,几十年来,Java比其他语言更常名列榜首,Java因为它拥有可移植性.可扩展性和庞大的用户社区,所以许多知名互联网公司使用Java ...
- The Mean of the Sample Mean|Standard Deviation of the Sample Mean|SE
7.2 The Mean and Standard Deviation of the Sample Mean Recall that the mean of a variable is denote ...
- Ananconda常用指令
Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项,可用于解决开发过程中遇到python版本需要切换的问题. conda有一点好处是,如 ...
- mysql 优化2 慢查询
默认情况下mysql不记录慢查询日志,需要在启动的时候指定 bin\mysqld.exe - -slow-query-log 通过慢查询日志定位执行效率较低的SQL语句.慢查询日志记录了所有执行时间超 ...
- [LC] 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 吴裕雄--天生自然Android开发学习:1.2.2 使用Android Studio开发Android APP
1.下载Android Studio 官网下载:Android Studio for Window ... :http://developer.android.com/sdk/installing/s ...
- Chrome开发者调试工具
参考资料 Chrome Console不完全指南 Chrome使用技巧 Chrome开发工具详解 结束语 工欲善其事,必先利其器.
- Oracle 10G 服务端的升级
第一步:备份 rman target / backup full database plus archivelog; 第二步:升级 解压升级包到soft目录下,修改所有者 chown -R oracl ...
- python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案
人生苦短,我用python!为什么很多人喜欢用python,因为包多呀,各种调包.但是调包有的时候也调的闹心,因为安装包不是失败就是很慢,很影响自己的工作进度,这里给出一个pip快速安装工具包的办法, ...