substring

計算機術語

public String substring(int beginIndex)

返回一個新的字元串,它是此字元串的一個子字元串。該子字元串始於指定索引處的字元,一直到此字元串末尾。在SQLserver資料庫中,用於截取字元串的某部分。

Java簡介


例如:/
參數:
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大於字元串長度。
上面返回字元串:"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();
}
}
}
程序輸出的結果:
Aquickfoxisjumpingoverthelazydog
Aquickfoxis

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。

資料來源

C++Builder2007幫助文檔(原文是英文的)

SQLserver資料庫


用於截取字元串的某部分,其基本語法為 select substring(字元串或者列名,起始位置,截取長度) from 表。
substring
substring
例如:
select substring(‘ename’,2,2) from emp;
結果為‘am’。