函数和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
发布评论