C/C++中substr函数的应用(简单讲解)
来源:互联网 发布:手机淘宝怎么二次追评 编辑:程序博客网 时间:2024/05/16 07:17
该文章转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/6763801.html
substr(字符串,截取开始位置,截取长度) //返回截取的字
substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串
substr('Hello World',1,1) //返回结果为 'H' *0和1都是表示截取的开始位置为第一个字符
substr('Hello World',2,4) //返回结果为 'ello'
substr('Hello World',-3,3)//返回结果为 'rld' *负数(-i)表示截取的开始位置为字符串右端向左数第i个字符
测试:
select substr('Hello World',-3,3) value from dual;
附:java中substring(index1,index2)的简单用法
作用:从字符串索引(下标)为index1的字符开始截取长度为index2-index1 的字符串。
String str="Hello World";
System.out.println(str.substring(0,5));
打印结果为:Hello
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <assert.h>char* mysubstr(char* srcstr, int offset, int length){ assert(length > 0); assert(srcstr != NULL); int total_length = strlen(srcstr);//首先获取srcstr的长度 //判断srcstr的长度减去需要截取的substr开始位置之后,剩下的长度 //是否大于指定的长度length,如果大于,就可以取长度为length的子串 //否则就把从开始位置剩下的字符串全部返回。 int real_length = ((total_length - offset) >= length ? length : (total_length - offset)) + 1; char *tmp; if (NULL == (tmp=(char*) malloc(real_length * sizeof(char)))) { printf("Memory overflow . \n"); exit(0); } strncpy(tmp, srcstr+offset, real_length - 1); tmp[real_length - 1] = '\0'; return tmp;}int main(){ char srcstr[] = "this is a test string!"; char* tmp = mysubstr(srcstr, 8, 8); printf("TEST: result = %s\n", tmp); free(tmp); return 0;}
阅读全文
0 0
- C/C++中substr函数的应用(简单讲解)
- 【C++】C++中substr的用法
- atoi函数与scanf中%*c及%[^\n]的简单讲解
- 【C++】纯虚函数的简单应用。
- 【c++】函数模板的简单应用
- substr 截取函数的应用
- C语言中随机函数的应用
- c语言中函数指针的应用
- c语言中函数的简单介绍
- C语言中scanf()函数讲解
- C语言的结构体简单讲解
- c语言优缺点的简单讲解
- c语言字符串的一些简单讲解
- C的函数,变量声明,malloc,MessageBox,sprintf,简单应用
- realloc函数的简单应用-c语言程序
- Oracle的substr函数简单用法
- Oracle的substr函数简单用法
- Oracle的substr函数简单用法
- Ubuntu 16.04 常用软件安装
- 锚链接跳转+滚动效果
- spark源码学习
- 【Scikit-Learn 中文文档】朴素贝叶斯
- 【大连】17年12月20日前,全市驾校全部安装计时培训终端!
- C/C++中substr函数的应用(简单讲解)
- 欢迎使用CSDN-markdown编辑器
- Java 内存区域和GC机制
- 人脸识别迁移学习的应用
- 【正则】正则表达式入门(1)数字和通配符
- 你真的理解AccessibilityService吗
- SpringBoot构建微服务实战 之 Configuration(三)
- 放大镜效果的制作
- idea中的代码简单调试debug