String.StartsWith メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
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);
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
されている場合は、現在のカルチャが使用されます。
戻り値
value
パラメーターがこの文字列の先頭と一致するかどうかを true
します。それ以外の場合は、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
の比較方法を決定する列挙値の 1 つ。
戻り値
このインスタンスが value
で始まるかどうかを true
します。それ以外の場合は、false
します。
- 属性
例外
value
は null
です。
comparisonType
は StringComparison 値ではありません。
例
次の例では、"The" という単語で始まる長い文字列の先頭にある文字列 "the" を検索します。 この例の出力に示すように、カルチャを区別せずに大文字と小文字を区別する比較を実行する StartsWith(String, StringComparison) メソッドの呼び出しは文字列と一致しませんが、カルチャと大文字と小文字を区別しない比較を実行する呼び出しは文字列と一致します。
using namespace System;
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
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
次の例では、文字列が特定の部分文字列で始まるかどうかを判断します。 2 次元文字列配列を初期化します。 2 番目のディメンションの最初の要素には文字列が含まれており、2 番目の要素には最初の文字列の先頭で検索する文字列が含まれています。 結果は、カルチャの選択、大文字と小文字の区別が無視されるかどうか、および序数比較が実行されるかどうかによって影響を受けます。 文字列インスタンスに合字が含まれている場合、カルチャに依存する比較とその連続する文字が正常に一致することに注意してください。
using namespace System;
void main()
{
array<String^, 2>^ strings = gcnew array<String^, 2> { {"ABCdef", "abc" },
{"ABCdef", "abc" }, {"œil","oe" },
{ "læring}", "lae" } };
for (int ctr1 = strings->GetLowerBound(0); ctr1 <= strings->GetUpperBound(0); ctr1++)
{
for each (String^ cmpName in Enum::GetNames(StringComparison::typeid))
{
StringComparison strCmp = (StringComparison) Enum::Parse(StringComparison::typeid,
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)
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.InvariantCulture、または StringComparison.Ordinal) ことも、大文字と小文字 (StringComparison.CurrentCultureIgnoreCase、StringComparison.InvariantCultureIgnoreCase、StringComparison.OrdinalIgnoreCase) を無視することもできます。
こちらもご覧ください
適用対象
StartsWith(Char)
この文字列インスタンスが指定した文字で始まるかどうかを判断します。
public:
bool StartsWith(char value);
public bool StartsWith (char value);
member this.StartsWith : char -> bool
Public Function StartsWith (value As Char) As Boolean
パラメーター
- value
- Char
比較する文字。
戻り値
value
がこの文字列の先頭と一致する場合に true
します。それ以外の場合は、false
します。
注釈
このメソッドは、序数 (大文字と小文字を区別し、カルチャを区別しない) 比較を実行します。
適用対象
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
比較する文字列。
戻り値
value
がこの文字列の先頭と一致する場合に true
します。それ以外の場合は、false
します。
例外
value
は null
です。
例
次の例では、StartsWith(String) メソッドを使用して文字列の先頭から HTML 開始タグを削除する StripStartTags
メソッドを定義します。
StripStartTags
メソッドは、行の先頭にある複数の HTML 開始タグが確実に削除されるように再帰的に呼び出されることに注意してください。 この例では、文字列に埋め込まれた HTML タグは削除されません。
using namespace System;
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;
}
int main()
{
array<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("---------------------");
for each (String^ 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.
for each (String^ s in strSource)
Console::WriteLine(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
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) である必要があります。また、この同じインスタンスへの参照であるか、このインスタンスの先頭と一致している必要があります。
このメソッドは、現在のカルチャを使用して単語 (大文字と小文字が区別され、カルチャに依存) の比較を実行します。
注意 (呼び出し元)
文字列を使用するためのベスト プラクティス comparisonType
パラメーターの値を使用して StartsWith(String, StringComparison) メソッドのオーバーロードを CurrentCulture 呼び出すことによって、意図を明示的に通知します。 言語に対応した比較が必要ない場合は、Ordinalの使用を検討してください。
こちらもご覧ください
適用対象
.NET