
substring
substring
public String substring(int beginIndex)
返回一个新的字元串,它是此字元串的一个子字元串。该子字元串始于指定索引处的字元,一直到此字元串末尾。
基本介绍
- 中文名:截取字元串
- 外文名:substring
Java
简介
例如:
"unhappy".substring(2)returns"happy""Harbison".substring(3)returns"bison""emptiness".substring(9)returns""(anemptystring)
参数:
beginIndex - 开始处的索引(包括)。
返回:
指定的子字元串。
抛出:
IndexOutOfBoundsException - 如果 beginIndex 为负或大于此 String 对象的长度。
--------------------------------------------------------------------------------
substring
public String substring(int beginIndex, int endIndex)
返回一个新字元串,它是此字元串的一个子字元串。该子字元串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束。
示例:
"hamburger".substring(3,8) returns "burge"
"smiles".substring(0,5) returns "smile"
参数:
beginIndex - 开始处的索引(包括)。
endindex 结尾处索引(不包括)。
返回:
指定的子字元串。
抛出:
IndexOutOfBoundsException - 如果 beginIndex 为负,或length大于字元串长度。
javascript示例
<scripttype="text/javascript">var str="Helloworld!"document.write(str.substring(1,3));</script>
上面返回字元串:"el";
str.substring(1,2) //返回e
str.substring(1) //返回"elloworld";
还有此函式中会出现奇怪的现象,当出现str.substring(5,0);
这又是怎幺回事,不过返回的是"Hello",
str.substring(5,1) //返回"ello",截去了第一位,返回余下的.
可见substring(start,end),可以有不同的说明,即start可以是要返回的长度,end是所要去掉的多少个字元(从首位开始).
在JS中,substr(start,length),用得较方便.
C#中
变数.Substring(参数1,参数2);
截取字串的一部分,参数1为左起始位数,参数2为截取几位。
如:string s1 = str.Substring(0,2);
C#中有两个重载函式
举例如下代码,VS2005编译通过
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacesln_sub{classProgram{staticvoidMain(string[]args){stringmyString="Aquickfoxisjumpingoverthelazydog";//Substring()在C#中有两个重载函式//分别如下示例stringsubString1=myString.Substring(0);//如果传入参数为一个长整,且大于等于0,//则以这个长整的位置为起始,//截取之后余下所有作为字串.//如若传入值小于0,//系统会抛出ArgumentOutOfRange异常//表明参数範围出界stringsubString2=myString.Substring(0,11);//如果传入了两个长整参数,//前一个为参数子串在原串的起始位置//后一个参数为子串的长度//如不合条件同样出现上述异常Console.WriteLine(subString1);Console.WriteLine(subString2);Console.ReadLine();}}}
程式输出的结果:
AquickfoxisjumpingoverthelazydogAquickfoxis
js用法
在JS中, 函式声明: stringObject.substring(start,stop)
start是在原字元串检索的开始位置,stop是检索的终止位置,返回结果中不包括stop所指字元.
CB用法
用途
Returns the substring at the specified location within a String object.
用法举例
strVariable.substring(start, end)
"String Literal".substring(start, end)
用法说明:返回一个字串,其中start是起始的index,end是终止的index,返回的字串包含起始index的字元,但是不包含end的字元。这个是string类下的一个method。
用法实例
functionSubstringDemo(){varss;//Declarevariables.vars="TheraininSpainfallsmainlyintheplain..";ss=s.substring(12,17);//Getsubstring.return(ss);//Returnsubstring.}
资料来源
C++Builder2007帮助文档(原文是英文的)
网上资料
ms-help://borland.bds5/script56/html/9cf9a005-cbe3-42fd-828b-57a39f54224c.htm