substr函数和find

编程入门 行业动态 更新时间:2024-10-28 13:18:58

substr<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数和find"/>

substr函数和find

substr函数是一个字符串截取函数,它返回的是截取的字符串。
函数定义如下:

/** 截取字符串(字符串位置从1开始,而不是从0开始)* @param string 源字符串* @param position 检索位置,参数为正时,从左向右检索,参数为负时,从右向左检索* @param substring_length 要截取的长度,可省略(默认从position位开始截取全部),值小于1时返回空字符串* @return 返回截取的字符串*/
substr(string, position, substring_length);

例如:

SELECT SUBSTR('hello world', 2) FROM DUAL;        --结果:ello world
SELECT SUBSTR('hello world', -2) FROM DUAL;       --结果:ld
SELECT SUBSTR('hello world', 4, 4) FROM DUAL;     --结果:lo w
SELECT SUBSTR('hello world', -4, 3) FROM DUAL;    --结果:orl
SELECT SUBSTR('hello world', 4, -1) FROM DUAL;    --结果:空字符串

字符串查找方面的部分函数用法简介:

1.find()

查找第一次出现的目标字符串:

#include<iostream>
#include<csdtio>
using namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2) ;   //在S1中查找子串S2
cout<<ans<<endl;
system("pause");
}

说明:如果查找成功则输出查找到的第一个位置,否则返回-1;

查找从指定位置开始的第一次出现的目标字符串:

#include<iostream>
#include<csdtio>
using namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2, 2) ;   //从S1的第二个字符开始查找子串S2
cout<<ans<<endl;
system("pause");
}

更多推荐

substr函数和find

本文发布于:2023-07-28 15:39:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1238194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   substr   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!