substring函数的用法 函数substr
大家好,感谢邀请,今天来为大家分享一下substring函数的用法的问题,以及和函数substr的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!
c中substring的用法
c中substring的用法的用法你知道吗?下面我就跟你们详细介绍下c中substring的用法的用法,希望对你们有用。
c中substring的用法的用法如下:
String.SubString(int index,int length)
index:开始位置,从0开始
length:你要取的子字符串的长度
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace str_sub
{
class Program
{
static void Main(string[] args)
{
string myString="Hello Word!";
//Substring()在C#中有两个重载函数
//分别如下示例
string subString1= myString.Substring(0);
//如果传入参数为一个长整,且大于等于0,
//则以这个长整的位置为起始,
//截取之后余下所有作为字串.
//如若传入值小于0,
//系统会抛出ArgumentOutOfRange异常
//表明参数范围出界
string subString2= myString.Substring(0, 5);
//如果传入了两个长整参数,
//前一个为参数子串在原串的起始位置
//后一个参数为子串的长度
//如不合条件同样出现上述异常
Console.WriteLine(subString1);
Console.WriteLine(subString2);
Console.ReadLine();
}
}
}
程序输出的结果:
Hello Word!
Hello
substr函数怎么用
substr方法
返回一个从指定位置开始,并具有指定长度的子字符串。
参数
start
必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。
length
可选项。返回的子字符串中包含的字符数。
备注
如果 length为 0或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。
示例
下面的示例阐释了 substr方法的用法。
function SubstrDemo(){
var s, ss;//Declare variables.
var s="The rain in Spain falls mainly in the plain.";
ss= s.substr(12, 5);//Get substring.
return(ss);// Returns"Spain".
----------------------------------------------crazyghost_von补充-----------------------------------------------------------------------
s.substr(12)的结果是 Spain falls mainly in the plain.
----------------------------------------------------------------------------------------------------------------------------------------------
在oracle中的用法:
SUBSTR(:NEW.FLAGSTATUS,17,1)
其中第一一次是是(串,开始,长度)返回子串。
c++中 string 类的find函数的用法
string类的查找函数:
int find(char c, int pos= 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char*s, int pos= 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char*s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string&s, int pos= 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos= npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char*s, int pos= npos) const;
int rfind(const char*s, int pos, int n= npos) const;
int rfind(const string&s,int pos= npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos= 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char*s, int pos= 0) const;
int find_first_of(const char*s, int pos, int n) const;
int find_first_of(const string&s,int pos= 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
int find_first_not_of(char c, int pos= 0) const;
int find_first_not_of(const char*s, int pos= 0) const;
int find_first_not_of(const char*s, int pos,int n) const;
int find_first_not_of(const string&s,int pos= 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_last_of(char c, int pos= npos) const;
int find_last_of(const char*s, int pos= npos) const;
int find_last_of(const char*s, int pos, int n= npos) const;
int find_last_of(const string&s,int pos= npos) const;
int find_last_not_of(char c, int pos= npos) const;
int find_last_not_of(const char*s, int pos= npos) const;
int find_last_not_of(const char*s, int pos, int n) const;
int find_last_not_of(const string&s,int pos= npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
OK,关于substring函数的用法和函数substr的内容到此结束了,希望对大家有所帮助。