获取以strbegin为开始strend为结束的中间字符
2010-02-08 09:50
225 查看
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *string=NULL;
char *ptr, c = 'r',*result;
char *str1 = "begin123456end", *strbegin = "begin",*strend = "end";
int beginindex,endindex,beginstrlength=sizeof(strbegin);
ptr = strstr(str1, strbegin); //获取开始匹配字符串的指针
beginindex=ptr-str1;//开始字符串坐标
ptr = strstr(str1, strend); //获取开始匹配字符串的指针
endindex=ptr-str1;//结束字符串的左边
int n=endindex-beginindex-beginstrlength;//个数获取开始坐标同结束坐标中间的字符
if(n>0) //考虑2个字符串之间是否有字符
{
string=(char*)malloc((n)*sizeof(char));
//动态申请数值的大小考虑strncpy不一定用null作为结束所以申请空间多一个指定用/0结束
strncpy(string, str1+beginindex+beginstrlength+1, n-1); //
string[n-1]='/0';
printf("%s/n", string);
free(string);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *string=NULL;
char *ptr, c = 'r',*result;
char *str1 = "begin123456end", *strbegin = "begin",*strend = "end";
int beginindex,endindex,beginstrlength=sizeof(strbegin);
ptr = strstr(str1, strbegin); //获取开始匹配字符串的指针
beginindex=ptr-str1;//开始字符串坐标
ptr = strstr(str1, strend); //获取开始匹配字符串的指针
endindex=ptr-str1;//结束字符串的左边
int n=endindex-beginindex-beginstrlength;//个数获取开始坐标同结束坐标中间的字符
if(n>0) //考虑2个字符串之间是否有字符
{
string=(char*)malloc((n)*sizeof(char));
//动态申请数值的大小考虑strncpy不一定用null作为结束所以申请空间多一个指定用/0结束
strncpy(string, str1+beginindex+beginstrlength+1, n-1); //
string[n-1]='/0';
printf("%s/n", string);
free(string);
}
return 0;
}
相关文章推荐
- c# 用正则表达式获取开始和结束字符串中间的值
- 指定开始字符串和结束字符串,截取中间的字符
- 获取网页中匹配代码的正则 (获得字符串中开始和结束字符串中间得值)
- c# 用正则表达式获取开始和结束字符串中间的值
- js方法中获取开始标签和结束标签中间插入内容的方式(innerHTML/html())
- C#: 获取网页中匹配代码的正则 (获得字符串中开始和结束字符串中间得值)
- C# :从一段字符串中,输入开始和结束的字符,取中间的字符?
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- java 获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- java获取各种格式的时间,获取昨天明天日期,获取一天的开始结束时间
- C#获取指定月指定周的日期范围 根据当前时间获取本月开始日期和结束日期
- java中 获取天,周,月,年的开始时间和结束时间
- SQL : 开始日期和结束日期,SQL找出中间的所有日期. 关于天数。
- 根据传入的年份和月份获取该月属于本年的第几周和每周的开始和结束日期
- java Calendar 日期获取工具类,昨日开始时间,昨日结束时间,当前周周一日期,当前时间上月第一天,获取当前年第一天
- Java获取的一天、本星期、这个月、本季度、一年等 开始和结束时间
- 获取当天的开始时间、结束时间等等的一个工具类
- PHP 获取一年中的每星期的开始日期和结束日期
- PHP 获取一年中的每星期的开始日期和结束日期
- php 获取本周的开始时间和结束时间