String.StartsWith 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| StartsWith(String, Boolean, CultureInfo) |
判斷該字串實例的開頭是否與指定字串相符,並使用指定的文化進行比較。 |
| StartsWith(String, StringComparison) |
透過指定的比較選項,判斷該字串實例的開頭是否與指定字串相符。 |
| StartsWith(Char) |
判斷此字串實例是否以指定字元開頭。 |
| StartsWith(String) |
判斷此字串實例的開頭是否與指定字串相符。 |
StartsWith(String, Boolean, CultureInfo)
判斷該字串實例的開頭是否與指定字串相符,並使用指定的文化進行比較。
public:
bool StartsWith(System::String ^ value, bool ignoreCase, System::Globalization::CultureInfo ^ culture);
public bool StartsWith(string value, bool ignoreCase, System.Globalization.CultureInfo culture);
member this.StartsWith : string * bool * System.Globalization.CultureInfo -> bool
Public Function StartsWith (value As String, ignoreCase As Boolean, culture As CultureInfo) As Boolean
參數
- value
- String
用來比較的線。
- ignoreCase
- Boolean
true在比較時忽略格;否則,。 false
- culture
- CultureInfo
文化資訊決定了這串的 value 比較方式。 如果 culture 為 null,則會使用目前的文化特性。
傳回
true如果參數value與該字串的開頭相符;否則,。 false
例外狀況
value 是 null。
範例
以下範例判斷一個字串是否出現在另一個字串的開頭。 此 StartsWith 方法會多次使用大小寫敏感性、不敏感性及影響搜尋結果的不同培養條件。
// This code example demonstrates the
// System.String.StartsWith(String, ..., CultureInfo) method.
using System;
using System.Threading;
using System.Globalization;
class Sample
{
public static void Main()
{
string msg1 = "Search for the target string \"{0}\" in the string \"{1}\".\n";
string msg2 = "Using the {0} - \"{1}\" culture:";
string msg3 = " The string to search ends with the target string: {0}";
bool result = false;
CultureInfo ci;
// Define a target string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
string capitalARing = "\u00c5";
// Define a string to search.
// The result of combining the characters LATIN SMALL LETTER A and COMBINING
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
string aRingXYZ = "\u0061\u030a" + "xyz";
// Clear the screen and display an introduction.
Console.Clear();
// Display the string to search for and the string to search.
Console.WriteLine(msg1, capitalARing, aRingXYZ);
// Search using English-United States culture.
ci = new CultureInfo("en-US");
Console.WriteLine(msg2, ci.DisplayName, ci.Name);
Console.WriteLine("Case sensitive:");
result = aRingXYZ.StartsWith(capitalARing, false, ci);
Console.WriteLine(msg3, result);
Console.WriteLine("Case insensitive:");
result = aRingXYZ.StartsWith(capitalARing, true, ci);
Console.WriteLine(msg3, result);
Console.WriteLine();
// Search using Swedish-Sweden culture.
ci = new CultureInfo("sv-SE");
Console.WriteLine(msg2, ci.DisplayName, ci.Name);
Console.WriteLine("Case sensitive:");
result = aRingXYZ.StartsWith(capitalARing, false, ci);
Console.WriteLine(msg3, result);
Console.WriteLine("Case insensitive:");
result = aRingXYZ.StartsWith(capitalARing, true, ci);
Console.WriteLine(msg3, result);
}
}
/*
Note: This code example was executed on a console whose user interface
culture is "en-US" (English-United States).
Search for the target string "Å" in the string "a°xyz".
Using the English (United States) - "en-US" culture:
Case sensitive:
The string to search ends with the target string: False
Case insensitive:
The string to search ends with the target string: True
Using the Swedish (Sweden) - "sv-SE" culture:
Case sensitive:
The string to search ends with the target string: False
Case insensitive:
The string to search ends with the target string: False
*/
// This code example demonstrates the
// System.String.StartsWith(String, ..., CultureInfo) method.
open System
open System.Globalization
[<EntryPoint>]
let main _ =
let msg1 = "Search for the target string \"{0}\" in the string \"{1}\".\n"
let msg2 = "Using the {0} - \"{1}\" culture:"
let msg3 = " The string to search ends with the target string: {0}"
// Define a target string to search for.
// U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
let capitalARing = "\u00c5"
// Define a string to search.
// The result of combining the characters LATIN SMALL LETTER A and COMBINING
// RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character
// LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
let aRingXYZ = "\u0061\u030a" + "xyz"
// Clear the screen and display an introduction.
Console.Clear()
// Display the string to search for and the string to search.
Console.WriteLine(msg1, capitalARing, aRingXYZ)
// Search using English-United States culture.
let ci = CultureInfo "en-US"
Console.WriteLine(msg2, ci.DisplayName, ci.Name)
printfn "Case sensitive:"
let result = aRingXYZ.StartsWith(capitalARing, false, ci)
Console.WriteLine(msg3, result)
printfn "Case insensitive:"
let result = aRingXYZ.StartsWith(capitalARing, true, ci)
Console.WriteLine(msg3, result)
printfn ""
// Search using Swedish-Sweden culture.
let ci = CultureInfo "sv-SE"
Console.WriteLine(msg2, ci.DisplayName, ci.Name)
printfn "Case sensitive:"
let result = aRingXYZ.StartsWith(capitalARing, false, ci)
Console.WriteLine(msg3, result)
printfn "Case insensitive:"
let result = aRingXYZ.StartsWith(capitalARing, true, ci)
Console.WriteLine(msg3, result)
0
(*
Note: This code example was executed on a console whose user interface
culture is "en-US" (English-United States).
Search for the target string "Å" in the string "a°xyz".
Using the English (United States) - "en-US" culture:
Case sensitive:
The string to search ends with the target string: False
Case insensitive:
The string to search ends with the target string: True
Using the Swedish (Sweden) - "sv-SE" culture:
Case sensitive:
The string to search ends with the target string: False
Case insensitive:
The string to search ends with the target string: False
*)
' This code example demonstrates the
' System.String.StartsWith(String, ..., CultureInfo) method.
Imports System.Threading
Imports System.Globalization
Class Sample
Public Shared Sub Main()
Dim msg1 As String = "Search for the target string ""{0}"" in the string ""{1}""." & vbCrLf
Dim msg2 As String = "Using the {0} - ""{1}"" culture:"
Dim msg3 As String = " The string to search ends with the target string: {0}"
Dim result As Boolean = False
Dim ci As CultureInfo
' Define a target string to search for.
' U+00c5 = LATIN CAPITAL LETTER A WITH RING ABOVE
Dim capitalARing As String = "Å"
' Define a string to search.
' The result of combining the characters LATIN SMALL LETTER A and COMBINING
' RING ABOVE (U+0061, U+030a) is linguistically equivalent to the character
' LATIN SMALL LETTER A WITH RING ABOVE (U+00e5).
Dim aRingXYZ As String = "å" & "xyz"
' Clear the screen and display an introduction.
Console.Clear()
' Display the string to search for and the string to search.
Console.WriteLine(msg1, capitalARing, aRingXYZ)
' Search using English-United States culture.
ci = New CultureInfo("en-US")
Console.WriteLine(msg2, ci.DisplayName, ci.Name)
Console.WriteLine("Case sensitive:")
result = aRingXYZ.StartsWith(capitalARing, False, ci)
Console.WriteLine(msg3, result)
Console.WriteLine("Case insensitive:")
result = aRingXYZ.StartsWith(capitalARing, True, ci)
Console.WriteLine(msg3, result)
Console.WriteLine()
' Search using Swedish-Sweden culture.
ci = New CultureInfo("sv-SE")
Console.WriteLine(msg2, ci.DisplayName, ci.Name)
Console.WriteLine("Case sensitive:")
result = aRingXYZ.StartsWith(capitalARing, False, ci)
Console.WriteLine(msg3, result)
Console.WriteLine("Case insensitive:")
result = aRingXYZ.StartsWith(capitalARing, True, ci)
Console.WriteLine(msg3, result)
End Sub
End Class
'
'Note: This code example was executed on a console whose user interface
'culture is "en-US" (English-United States).
'
'Search for the target string "Å" in the string "a°xyz".
'
'Using the English (United States) - "en-US" culture:
'Case sensitive:
' The string to search ends with the target string: False
'Case insensitive:
' The string to search ends with the target string: True
'
'Using the Swedish (Sweden) - "sv-SE" culture:
'Case sensitive:
' The string to search ends with the target string: False
'Case insensitive:
' The string to search ends with the target string: False
'
備註
此方法將 value 參數與該字串開頭長度相同的 value子串比較,並回傳一個表示兩者是否相等的值。 要相等,必須 value 是空字串(),String.Empty必須是指向同一實例的參考,或必須與該實例的開頭相符。
此方法利用指定的套管與培養進行比較。
適用於
StartsWith(String, StringComparison)
透過指定的比較選項,判斷該字串實例的開頭是否與指定字串相符。
public:
bool StartsWith(System::String ^ value, StringComparison comparisonType);
public bool StartsWith(string value, StringComparison comparisonType);
[System.Runtime.InteropServices.ComVisible(false)]
public bool StartsWith(string value, StringComparison comparisonType);
member this.StartsWith : string * StringComparison -> bool
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.StartsWith : string * StringComparison -> bool
Public Function StartsWith (value As String, comparisonType As StringComparison) As Boolean
參數
- value
- String
用來比較的線。
- comparisonType
- StringComparison
這是決定字串 和 value 如何比較的列舉值之一。
傳回
true 若此實例以 value; 開頭,否則, false。
- 屬性
例外狀況
value 是 null。
comparisonType 不是一個 StringComparison 數值。
範例
以下範例是在一個較長字串開頭的字串「the」中搜尋字串「the」。 如範例輸出所示, StartsWith(String, StringComparison) 呼叫執行文化不敏感但大小寫區分比較的方法,無法匹配該字串;而呼叫進行文化與大小寫不區分比較的呼叫則匹配該字串。
using System;
public class Example
{
public static void Main()
{
String title = "The House of the Seven Gables";
String searchString = "the";
StringComparison comparison = StringComparison.InvariantCulture;
Console.WriteLine("'{0}':", title);
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison));
comparison = StringComparison.InvariantCultureIgnoreCase;
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison));
}
}
// The example displays the following output:
// 'The House of the Seven Gables':
// Starts with 'the' (InvariantCulture comparison): False
// Starts with 'the' (InvariantCultureIgnoreCase comparison): True
open System
let title = "The House of the Seven Gables"
let searchString = "the"
let comparison = StringComparison.InvariantCulture
printfn $"'{title}':"
printfn $" Starts with '{searchString}' ({comparison:G} comparison): {title.StartsWith(searchString, comparison)}"
let comparison2 = StringComparison.InvariantCultureIgnoreCase
printfn $" Starts with '{searchString}' ({comparison2:G} comparison): {title.StartsWith(searchString, comparison2)}"
// The example displays the following output:
// 'The House of the Seven Gables':
// Starts with 'the' (InvariantCulture comparison): False
// Starts with 'the' (InvariantCultureIgnoreCase comparison): True
Module Example
Public Sub Main()
Dim title As String = "The House of the Seven Gables"
Dim searchString As String = "the"
Dim comparison As StringComparison = StringComparison.InvariantCulture
Console.WriteLine("'{0}':", title)
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison))
comparison = StringComparison.InvariantCultureIgnoreCase
Console.WriteLine(" Starts with '{0}' ({1:G} comparison): {2}",
searchString, comparison,
title.StartsWith(searchString, comparison))
End Sub
End Module
' The example displays the following output:
' 'The House of the Seven Gables':
' Starts with 'the' (InvariantCulture comparison): False
' Starts with 'the' (InvariantCultureIgnoreCase comparison): True
以下範例判斷字串是否以特定子字串開頭。 它初始化一個二維字串陣列。 第二維的第一個元素包含一個字串,第二個元素則包含第一個字串開頭要搜尋的字串。 結果會受到培養物選擇、是否忽略個案,以及是否進行序數比較所影響。 請注意,當字串實例包含連字時,與其連續字元的文化敏感比較會成功匹配。
using System;
public class Example
{
public static void Main()
{
string[,] strings = { {"ABCdef", "abc" },
{"ABCdef", "abc" },
{"œil","oe" },
{ "læring}", "lae" } };
for (int ctr1 = strings.GetLowerBound(0); ctr1 <= strings.GetUpperBound(0); ctr1++)
{
foreach (string cmpName in Enum.GetNames(typeof(StringComparison)))
{
StringComparison strCmp = (StringComparison) Enum.Parse(typeof(StringComparison),
cmpName);
string instance = strings[ctr1, 0];
string value = strings[ctr1, 1];
Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)",
instance, value,
instance.StartsWith(value, strCmp),
strCmp);
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// œil starts with oe: True (CurrentCulture comparison)
// œil starts with oe: True (CurrentCultureIgnoreCase comparison)
// œil starts with oe: True (InvariantCulture comparison)
// œil starts with oe: True (InvariantCultureIgnoreCase comparison)
// œil starts with oe: False (Ordinal comparison)
// œil starts with oe: False (OrdinalIgnoreCase comparison)
//
// læring} starts with lae: True (CurrentCulture comparison)
// læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
// læring} starts with lae: True (InvariantCulture comparison)
// læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
// læring} starts with lae: False (Ordinal comparison)
// læring} starts with lae: False (OrdinalIgnoreCase comparison)
open System
let strings =
array2D
[ [ "ABCdef"; "abc" ]
[ "ABCdef"; "abc" ]
[ "œil"; "oe" ]
[ "læring}"; "lae" ] ]
for ctr1 = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
for cmpName in Enum.GetNames typeof<StringComparison> do
let strCmp = Enum.Parse(typeof<StringComparison>, cmpName) :?> StringComparison
let instance = strings[ctr1, 0]
let value = strings[ctr1, 1]
printfn $"{instance} starts with {value}: {instance.StartsWith(value, strCmp)} ({strCmp} comparison)"
printfn ""
// The example displays the following output:
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// ABCdef starts with abc: False (CurrentCulture comparison)
// ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
// ABCdef starts with abc: False (InvariantCulture comparison)
// ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
// ABCdef starts with abc: False (Ordinal comparison)
// ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
//
// œil starts with oe: True (CurrentCulture comparison)
// œil starts with oe: True (CurrentCultureIgnoreCase comparison)
// œil starts with oe: True (InvariantCulture comparison)
// œil starts with oe: True (InvariantCultureIgnoreCase comparison)
// œil starts with oe: False (Ordinal comparison)
// œil starts with oe: False (OrdinalIgnoreCase comparison)
//
// læring} starts with lae: True (CurrentCulture comparison)
// læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
// læring} starts with lae: True (InvariantCulture comparison)
// læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
// læring} starts with lae: False (Ordinal comparison)
// læring} starts with lae: False (OrdinalIgnoreCase comparison)
Module Example
Public Sub Main()
Dim strings(,) As String = { {"ABCdef", "abc" },
{"ABCdef", "abc" },
{"œil","oe" },
{ "læring}", "lae" } }
For ctr1 As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
For Each cmpName As String In [Enum].GetNames(GetType(StringComparison))
Dim strCmp As StringComparison = CType([Enum].Parse(GetType(StringComparison),
cmpName), StringComparison)
Dim instance As String = strings(ctr1, 0)
Dim value As String = strings(ctr1, 1)
Console.WriteLine("{0} starts with {1}: {2} ({3} comparison)",
instance, value,
instance.StartsWith(value, strCmp),
strCmp)
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' ABCdef starts with abc: False (CurrentCulture comparison)
' ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
' ABCdef starts with abc: False (InvariantCulture comparison)
' ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
' ABCdef starts with abc: False (Ordinal comparison)
' ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
'
' ABCdef starts with abc: False (CurrentCulture comparison)
' ABCdef starts with abc: True (CurrentCultureIgnoreCase comparison)
' ABCdef starts with abc: False (InvariantCulture comparison)
' ABCdef starts with abc: True (InvariantCultureIgnoreCase comparison)
' ABCdef starts with abc: False (Ordinal comparison)
' ABCdef starts with abc: True (OrdinalIgnoreCase comparison)
'
' œil starts with oe: True (CurrentCulture comparison)
' œil starts with oe: True (CurrentCultureIgnoreCase comparison)
' œil starts with oe: True (InvariantCulture comparison)
' œil starts with oe: True (InvariantCultureIgnoreCase comparison)
' œil starts with oe: False (Ordinal comparison)
' œil starts with oe: False (OrdinalIgnoreCase comparison)
'
' læring} starts with lae: True (CurrentCulture comparison)
' læring} starts with lae: True (CurrentCultureIgnoreCase comparison)
' læring} starts with lae: True (InvariantCulture comparison)
' læring} starts with lae: True (InvariantCultureIgnoreCase comparison)
' læring} starts with lae: False (Ordinal comparison)
' læring} starts with lae: False (OrdinalIgnoreCase comparison)
備註
該 StartsWith 方法會 value 將參數與該字串開頭的子字串比較,並回傳一個值以判斷兩者是否相等。 要相等,必須 value 是指向同一字串的參考,必須是空字串(“”),或必須與該字串的開頭相符。 方法所執行 StartsWith 的比較類型取決於參數 comparisonType 的值。 比較可使用當前文化(StringComparison.CurrentCulture 與 StringComparison.CurrentCultureIgnoreCase)或不變文化(StringComparison.InvariantCulture 與 StringComparison.InvariantCultureIgnoreCase)的慣例,或是逐字元比較碼點(StringComparison.Ordinal 或 StringComparison.OrdinalIgnoreCase)。 比較也可以是大小寫區分(StringComparison.CurrentCulture, , 或 StringComparison.Ordinal),或忽略大小寫(StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCaseStringComparison.OrdinalIgnoreCaseStringComparison.InvariantCulture, )。
另請參閱
適用於
StartsWith(String)
判斷此字串實例的開頭是否與指定字串相符。
public:
bool StartsWith(System::String ^ value);
public bool StartsWith(string value);
member this.StartsWith : string -> bool
Public Function StartsWith (value As String) As Boolean
參數
- value
- String
用來比較的線。
傳回
true若與value此字串的開頭相符;否則,。 false
例外狀況
value 是 null。
範例
以下範例定義了一種 StripStartTags 方法,利用該 StartsWith(String) 方法移除字串開頭的 HTML 起始標籤。 請注意,此 StripStartTags 方法是遞迴呼叫,以確保該行開頭的多個 HTML 起始標籤被移除。 範例中並未移除嵌入字串中的 HTML 標籤。
using System;
public class Example
{
public static void Main() {
string [] strSource = { "<b>This is bold text</b>", "<H1>This is large Text</H1>",
"<b><i><font color=green>This has multiple tags</font></i></b>",
"<b>This has <i>embedded</i> tags.</b>",
"<This line simply begins with a lesser than symbol, it should not be modified" };
// Display the initial string array.
Console.WriteLine("The original strings:");
Console.WriteLine("---------------------");
foreach (var s in strSource)
Console.WriteLine(s);
Console.WriteLine();
Console.WriteLine("Strings after starting tags have been stripped:");
Console.WriteLine("-----------------------------------------------");
// Display the strings with starting tags removed.
foreach (var s in strSource)
Console.WriteLine(StripStartTags(s));
}
private static string StripStartTags(string item)
{
// Determine whether a tag begins the string.
if (item.Trim().StartsWith("<")) {
// Find the closing tag.
int lastLocation = item.IndexOf( ">" );
// Remove the tag.
if (lastLocation >= 0) {
item = item.Substring( lastLocation + 1 );
// Remove any additional starting tags.
item = StripStartTags(item);
}
}
return item;
}
}
// The example displays the following output:
// The original strings:
// ---------------------
// <b>This is bold text</b>
// <H1>This is large Text</H1>
// <b><i><font color = green>This has multiple tags</font></i></b>
// <b>This has <i>embedded</i> tags.</b>
// <This line simply begins with a lesser than symbol, it should not be modified
//
// Strings after starting tags have been stripped:
// -----------------------------------------------
// This is bold text</b>
// This is large Text</H1>
// This has multiple tags</font></i></b>
// This has <i>embedded</i> tags.</b>
// <This line simply begins with a lesser than symbol, it should not be modified
let rec stripStartTags (item: string) =
// Determine whether a tag begins the string.
if item.Trim().StartsWith "<" then
// Find the closing tag.
let lastLocation = item.IndexOf ">"
// Remove the tag.
let item =
if lastLocation >= 0 then
item.Substring( lastLocation + 1 )
else
item
// Remove any additional starting tags.
stripStartTags item
else
item
let strSource =
[| "<b>This is bold text</b>"; "<H1>This is large Text</H1>"
"<b><i><font color=green>This has multiple tags</font></i></b>"
"<b>This has <i>embedded</i> tags.</b>"
"<This line simply begins with a lesser than symbol, it should not be modified" |]
// Display the initial string array.
printfn "The original strings:"
printfn "---------------------"
for s in strSource do
printfn $"{s}"
printfn ""
printfn "Strings after starting tags have been stripped:"
printfn "-----------------------------------------------"
// Display the strings with starting tags removed.
for s in strSource do
printfn $"{stripStartTags s}"
// The example displays the following output:
// The original strings:
// ---------------------
// <b>This is bold text</b>
// <H1>This is large Text</H1>
// <b><i><font color = green>This has multiple tags</font></i></b>
// <b>This has <i>embedded</i> tags.</b>
// <This line simply begins with a lesser than symbol, it should not be modified
//
// Strings after starting tags have been stripped:
// -----------------------------------------------
// This is bold text</b>
// This is large Text</H1>
// This has multiple tags</font></i></b>
// This has <i>embedded</i> tags.</b>
// <This line simply begins with a lesser than symbol, it should not be modified
Public Class Example
Public Shared Sub Main()
Dim strSource() As String = { "<b>This is bold text</b>",
"<H1>This is large Text</H1>",
"<b><i><font color = green>This has multiple tags</font></i></b>",
"<b>This has <i>embedded</i> tags.</b>",
"<This line simply begins with a lesser than symbol, it should not be modified" }
' Display the initial string array.
Console.WriteLine("The original strings:")
Console.WriteLine("---------------------")
For Each s In strSource
Console.WriteLine(s)
Next
Console.WriteLine()
Console.WriteLine("Strings after starting tags have been stripped:")
Console.WriteLine("-----------------------------------------------")
' Display the strings with starting tags removed.
For Each s In strSource
Console.WriteLine(StripStartTags(s))
Next
End Sub
Private Shared Function StripStartTags(item As String) As String
' Determine whether a tag begins the string.
If item.Trim().StartsWith("<") Then
' Find the closing tag.
Dim lastLocation As Integer = item.IndexOf(">")
If lastLocation >= 0 Then
' Remove the tag.
item = item.Substring((lastLocation + 1))
' Remove any additional starting tags.
item = StripStartTags(item)
End If
End If
Return item
End Function
End Class
' The example displays the following output:
' The original strings:
' ---------------------
' <b>This is bold text</b>
' <H1>This is large Text</H1>
' <b><i><font color = green>This has multiple tags</font></i></b>
' <b>This has <i>embedded</i> tags.</b>
' <This line simply begins with a lesser than symbol, it should not be modified
'
' Strings after starting tags have been stripped:
' -----------------------------------------------
' This is bold text</b>
' This is large Text</H1>
' This has multiple tags</font></i></b>
' This has <i>embedded</i> tags.</b>
' <This line simply begins with a lesser than symbol, it should not be modified
備註
此方法與 value 本實例開頭與 相同長度 value的子串比較,並回傳兩者是否相等的指示。 要相等,必須 value 是空字串(),String.Empty必須是指向同一實例的參考,或必須與該實例的開頭相符。
此方法利用當前文化進行詞彙(大小寫區分與文化敏感)比較。
給呼叫者的注意事項
如《 使用字串的最佳實務》所述,我們建議避免呼叫替換預設值的字串比較方法,而是呼叫需要明確指定參數的方法。 要利用當前文化的字串比較規則判斷字串是否以特定子字串開頭,請明確以參數值呼叫StartsWith(String, StringComparison)方法過載CurrentCulturecomparisonType來表明你的意圖。 如果你不需要語言感知比較,可以考慮使用 Ordinal。