String.Substring 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從這個執行個體擷取子字串。
這個成員是多載的。 如需這個成員的完整資訊,包含語法、使用方式和範例,請按一下多載清單中的名稱。
多載
Substring(Int32) |
從這個執行個體擷取子字串。 子字串會在指定的字元位置開始並繼續到字串的結尾。 |
Substring(Int32, Int32) |
從這個執行個體擷取子字串。 子字串起始於指定的字元位置,並且具有指定的長度。 |
Substring(Int32)
從這個執行個體擷取子字串。 子字串會在指定的字元位置開始並繼續到字串的結尾。
public:
System::String ^ Substring(int startIndex);
public string Substring (int startIndex);
member this.Substring : int -> string
Public Function Substring (startIndex As Integer) As String
參數
- startIndex
- Int32
這個執行個體中子字串之以零為起始的起始字元位置。
傳回
與這個執行個體中從 startIndex
開始之子字串相等的字串;如果 Empty 等於這個執行個體的長度,則為 startIndex
。
例外狀況
startIndex
小於零或大於此執行個體的長度。
範例
下列範例示範如何從字串取得子字串。
using namespace System;
using namespace System::Collections;
int main()
{
array<String^>^info = { "Name: Felica Walker", "Title: Mz.",
"Age: 47", "Location: Paris", "Gender: F"};
int found = 0;
Console::WriteLine("The initial values in the array are:");
for each (String^ s in info)
Console::WriteLine(s);
Console::WriteLine("\nWe want to retrieve only the key information. That is:");
for each (String^ s in info) {
found = s->IndexOf(": ");
Console::WriteLine(" {0}", s->Substring(found + 2));
}
}
// The example displays the following output:
// The initial values in the array are:
// Name: Felica Walker
// Title: Mz.
// Age: 47
// Location: Paris
// Gender: F
//
// We want to retrieve only the key information. That is:
// Felica Walker
// Mz.
// 47
// Paris
// F
string [] info = { "Name: Felica Walker", "Title: Mz.",
"Age: 47", "Location: Paris", "Gender: F"};
int found = 0;
Console.WriteLine("The initial values in the array are:");
foreach (string s in info)
Console.WriteLine(s);
Console.WriteLine("\nWe want to retrieve only the key information. That is:");
foreach (string s in info)
{
found = s.IndexOf(": ");
Console.WriteLine(" {0}", s.Substring(found + 2));
}
// The example displays the following output:
// The initial values in the array are:
// Name: Felica Walker
// Title: Mz.
// Age: 47
// Location: Paris
// Gender: F
//
// We want to retrieve only the key information. That is:
// Felica Walker
// Mz.
// 47
// Paris
// F
Public Class SubStringTest
Public Shared Sub Main()
Dim info As String() = { "Name: Felica Walker", "Title: Mz.",
"Age: 47", "Location: Paris", "Gender: F"}
Dim found As Integer = 0
Console.WriteLine("The initial values in the array are:")
For Each s As String In info
Console.WriteLine(s)
Next s
Console.WriteLine(vbCrLf + "We want to retrieve only the key information. That is:")
For Each s As String In info
found = s.IndexOf(": ")
Console.WriteLine(" {0}", s.Substring(found + 2))
Next s
End Sub
End Class
' The example displays the following output:
' The initial values in the array are:
' Name: Felica Walker
' Title: Mz.
' Age: 47
' Location: Paris
' Gender: F
'
' We want to retrieve only the key information. That is:
' Felica Walker
' Mz.
' 47
' Paris
' F
下列範例 Substring 會使用方法來分隔索引鍵/值組(以等於 ( "=" ) 字元分隔)。
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
"Title=Code Repository" };
foreach (var pair in pairs)
{
int position = pair.IndexOf("=");
if (position < 0)
continue;
Console.WriteLine("Key: {0}, Value: '{1}'",
pair.Substring(0, position),
pair.Substring(position + 1));
}
// The example displays the following output:
// Key: Color1, Value: 'red'
// Key: Color2, Value: 'green'
// Key: Color3, Value: 'blue'
// Key: Title, Value: 'Code Repository'
Module Example
Public Sub Main()
Dim pairs() As String = { "Color1=red", "Color2=green", "Color3=blue",
"Title=Code Repository" }
For Each pair In pairs
Dim position As Integer = pair.IndexOf("=")
If position < 0 then Continue For
Console.WriteLine("Key: {0}, Value: '{1}'",
pair.Substring(0, position),
pair.Substring(position + 1))
Next
End Sub
End Module
' The example displays the following output:
' Key: Color1, Value: 'red'
' Key: Color2, Value: 'green'
' Key: Color3, Value: 'blue'
' Key: Title, Value: 'Code Repository'
IndexOf方法是用來取得字串中等號字元的位置。 方法的呼叫會 Substring(Int32, Int32) 從字串中的第一個字元開始,以及針對方法呼叫所傳回的字元數進行擴充,以解壓縮索引鍵名稱 IndexOf 。 然後,對方法的呼叫會將指派給索引 Substring(Int32) 鍵的值解壓縮。 它會從等於字元的一個字元位置開始,並延伸至字串結尾。
備註
您可以呼叫 Substring(Int32) 方法,從起始于指定字元位置的字串解壓縮子字串,並在字串的結尾結束。 起始字元位置是以零為基底;換句話說,字串中的第一個字元位於索引0,而非索引1。 若要從指定的字元位置開始,並在字串結尾的結尾處解壓縮子字串,請呼叫 Substring(Int32, Int32) 方法。
注意
這個方法不會修改目前實例的值。 相反地,它會傳回新的字串,此字串會從 startIndex
目前字串的位置開始。
若要解壓縮以特定字元或字元序列開頭的子字串,請呼叫的方法(例如 IndexOf 或) IndexOf 來取得的值 startIndex
。 第二個範例說明這一點;它會解壓縮在 "=" 字元之後開始一個字元位置的索引鍵值。
如果 startIndex
等於零,則方法會傳回未變更的原始字串。
另請參閱
- Int32
- Concat(Object)
- Insert(Int32, String)
- Join(String, String[])
- Remove(Int32, Int32)
- Replace(Char, Char)
- Split(Char[])
- Trim(Char[])
適用於
Substring(Int32, Int32)
從這個執行個體擷取子字串。 子字串起始於指定的字元位置,並且具有指定的長度。
public:
System::String ^ Substring(int startIndex, int length);
public string Substring (int startIndex, int length);
member this.Substring : int * int -> string
Public Function Substring (startIndex As Integer, length As Integer) As String
參數
- startIndex
- Int32
這個執行個體中子字串之以零為起始的起始字元位置。
- length
- Int32
子字串中的字元數。
傳回
與長度為 length
且在這個執行個體中從 startIndex
開始之子字串相等的字串;如果 Empty 等於這個執行個體的長度且 startIndex
為零,則為 length
。
例外狀況
範例
下列範例說明簡單的 Substring(Int32, Int32) 方法呼叫,該方法會從第六個字元位置開始的字串中解壓縮兩個字元 (也就是在索引 5) 。
String value = "This is a string.";
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);
// The example displays the following output:
// is
Module Example
Public Sub Main()
Dim value As String = "This is a string."
Dim startIndex As Integer = 5
Dim length As Integer = 2
Dim substring As String = value.Substring(startIndex, length)
Console.WriteLine(substring)
End Sub
End Module
' The example displays the following output:
' is
下列範例會使用 Substring(Int32, Int32) 下列三種案例中的方法來隔離字串內的子字串。 在兩種情況下,會使用子字串進行比較,在第三個案例中會擲回例外狀況,因為指定的參數無效。
它會將字串中 (的單一字元和第三個位置解壓縮) ,並與 "c" 進行比較。 這種比較會傳回
true
。它會在索引 3) 字串 (的第四個位置開始解壓縮零個字元,並將它傳遞給 IsNullOrEmpty 方法。 這會傳回 true,因為呼叫 Substring 方法會傳回 String.Empty 。
它會嘗試從字串中的第四個位置開始解壓縮一個字元。 因為該位置沒有任何字元,方法呼叫會擲回例外狀況 ArgumentOutOfRangeException 。
string myString = "abc";
bool test1 = myString.Substring(2, 1).Equals("c"); // This is true.
Console.WriteLine(test1);
bool test2 = string.IsNullOrEmpty(myString.Substring(3, 0)); // This is true.
Console.WriteLine(test2);
try
{
string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
Console.WriteLine(str3);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
// The example displays the following output:
// True
// True
// Index and length must refer to a location within the string.
// Parameter name: length
Public Class Sample
Public Shared Sub Main()
Dim myString As String = "abc"
Dim test1 As Boolean = myString.Substring(2, 1).Equals("c") ' This is true.
Console.WriteLine(test1)
Dim test2 As Boolean = String.IsNullOrEmpty(myString.Substring(3, 0)) ' This is true.
Console.WriteLine(test2)
Try
Dim str3 As String = myString.Substring(3, 1) ' This throws ArgumentOutOfRangeException.
Console.WriteLine(str3)
Catch e As ArgumentOutOfRangeException
Console.WriteLIne(e.Message)
End Try
End Sub
End Class
' The example displays the following output:
' True
' True
' Index and length must refer to a location within the string.
' Parameter name: length
下列範例 Substring 會使用方法來分隔索引鍵/值組(以等於 ( "=" ) 字元分隔)。
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
"Title=Code Repository" };
foreach (var pair in pairs)
{
int position = pair.IndexOf("=");
if (position < 0)
continue;
Console.WriteLine("Key: {0}, Value: '{1}'",
pair.Substring(0, position),
pair.Substring(position + 1));
}
// The example displays the following output:
// Key: Color1, Value: 'red'
// Key: Color2, Value: 'green'
// Key: Color3, Value: 'blue'
// Key: Title, Value: 'Code Repository'
Module Example
Public Sub Main()
Dim pairs() As String = { "Color1=red", "Color2=green", "Color3=blue",
"Title=Code Repository" }
For Each pair In pairs
Dim position As Integer = pair.IndexOf("=")
If position < 0 then Continue For
Console.WriteLine("Key: {0}, Value: '{1}'",
pair.Substring(0, position),
pair.Substring(position + 1))
Next
End Sub
End Module
' The example displays the following output:
' Key: Color1, Value: 'red'
' Key: Color2, Value: 'green'
' Key: Color3, Value: 'blue'
' Key: Title, Value: 'Code Repository'
IndexOf方法是用來取得字串中等號字元的位置。 方法的呼叫會 Substring(Int32, Int32) 從字串中的第一個字元開始,以及針對方法呼叫所傳回的字元數進行擴充,以解壓縮索引鍵名稱 IndexOf 。 然後,對方法的呼叫會將指派給索引 Substring(Int32) 鍵的值解壓縮。 它會從等於字元的一個字元位置開始,並延伸至字串結尾。
備註
您可以呼叫 Substring(Int32, Int32) 方法,從起始于指定字元位置的字串解壓縮子字串,並在字串結尾之前結束。 起始字元位置是以零為基底;換句話說,字串中的第一個字元位於索引0,而非索引1。 若要從指定的字元位置開始解壓縮開頭的子字串,並繼續到字串的結尾,請呼叫 Substring(Int32) 方法。
注意
這個方法不會修改目前實例的值。 相反地,它會傳回新的字串,其中包含 length
從 startIndex
目前字串中的位置開始的字元。
length
參數代表要從目前的字串實例中解壓縮的字元總數。 這包括在索引處找到的起始字元 startIndex
。 換句話說, Substring 方法會嘗試將索引中的字元解壓縮 startIndex
至 index startIndex
+ length
-1。
若要解壓縮以特定字元或字元序列開頭的子字串,請呼叫的方法(例如 IndexOf 或) LastIndexOf 來取得的值 startIndex
。
如果子字串從延伸 startIndex
至指定的字元序列,您可以呼叫方法(例如 IndexOf 或) LastIndexOf 來取得結束字元或字元序列的索引。 然後,您可以將該值轉換成字串中的索引位置,如下所示:
如果您已搜尋要標示子字串結尾的單一字元,
length
參數等於endIndex
-startIndex
+ 1,其中endIndex
是或方法的傳回值 IndexOf IndexOf 。 下列範例會從字串中解壓縮 "b" 個字元的連續區塊。String s = "aaaaabbbcccccccdd"; Char charRange = 'b'; int startIndex = s.IndexOf(charRange); int endIndex = s.LastIndexOf(charRange); int length = endIndex - startIndex + 1; Console.WriteLine("{0}.Substring({1}, {2}) = {3}", s, startIndex, length, s.Substring(startIndex, length)); // The example displays the following output: // aaaaabbbcccccccdd.Substring(5, 3) = bbb
Module Example Public Sub Main() Dim s As String = "aaaaabbbcccccccdd" Dim charRange As Char = "b"c Dim startIndex As Integer = s.Indexof(charRange) Dim endIndex As Integer = s.LastIndexOf(charRange) Dim length = endIndex - startIndex + 1 Console.WriteLine("{0}.Substring({1}, {2}) = {3}", s, startIndex, length, s.Substring(startIndex, length)) End Sub End Module ' The example displays the following output: ' aaaaabbbcccccccdd.Substring(5, 3) = bbb
如果您已搜尋要標示子字串結尾的多個字元,
length
參數等於endIndex
+endMatchLength
-startIndex
,其中endIndex
是或方法的傳回值 IndexOf IndexOf ,而endMatchLength
則是標示子字串結尾的字元序列長度。 下列範例會解壓縮包含 XML 元素的文字區塊<definition>
。String s = "<term>extant<definition>still in existence</definition></term>"; String searchString = "<definition>"; int startIndex = s.IndexOf(searchString); searchString = "</" + searchString.Substring(1); int endIndex = s.IndexOf(searchString); String substring = s.Substring(startIndex, endIndex + searchString.Length - startIndex); Console.WriteLine("Original string: {0}", s); Console.WriteLine("Substring; {0}", substring); // The example displays the following output: // Original string: <term>extant<definition>still in existence</definition></term> // Substring; <definition>still in existence</definition>
Module Example Public Sub Main() Dim s As String = "<term>extant<definition>still in existence</definition></term>" Dim searchString As String = "<definition>" Dim startindex As Integer = s.IndexOf(searchString) searchString = "</" + searchString.Substring(1) Dim endIndex As Integer = s.IndexOf(searchString) Dim substring As String = s.Substring(startIndex, endIndex + searchString.Length - StartIndex) Console.WriteLine("Original string: {0}", s) Console.WriteLine("Substring; {0}", substring) End Sub End Module ' The example displays the following output: ' Original string: <term>extant<definition>still in existence</definition></term> ' Substring; <definition>still in existence</definition>
如果字元或字元序列未包含在子字串的結尾,
length
參數等於endIndex
-startIndex
,其中endIndex
是或方法的傳回值 IndexOf IndexOf 。
如果 startIndex
等於零且等於目前字串的長度,則方法會傳回未變更的原始字串。