String.StartsWith メソッド
このインスタンスの先頭が、指定した String と一致するかどうかを判断します。
Public Function StartsWith( _
ByVal value As String _) As Boolean
[C#]
public bool StartsWith(stringvalue);
[C++]
public: bool StartsWith(String* value);
[JScript]
public function StartsWith(
value : String) : Boolean;
パラメータ
- value
シークする String 。
戻り値
value がこの文字列の先頭と一致するかまたは Empty である場合は true 。それ以外の場合は false 。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | value が null 参照 (Visual Basic では Nothing) です。 |
解説
このメソッドは、文字列の先頭部分を比較し、この文字列が現在のインスタンスと一致するかどうかを判断し、この文字列とインスタンスの関係を示す Boolean 表現を返します。指定した文字列は、このインスタンスの先頭と一致しているか、 Empty と等価の空の文字列である必要があります。
このメソッドは、現在のカルチャを使用して、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行します。
使用例
StartsWith メソッドの使用方法については、次のコード例を参照してください。
Imports System
Public Class EndsWithTest
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"}
' process a string that contains html tags
' this sample does not remove embedded tags (tags in the middle of a line)
Console.WriteLine("The following lists the items before the ends have been stripped:")
Console.WriteLine("-----------------------------------------------------------------")
' print out the initial array of strings
Dim s As String
For Each s In strSource
Console.WriteLine(s)
Next s
Console.WriteLine()
Console.WriteLine("The following lists the items after the ends have been stripped:")
Console.WriteLine("----------------------------------------------------------------")
' print out the array of strings
For Each s In strSource
Console.WriteLine(StripStartTags(s))
Next s
End Sub 'Main
Private Shared Function StripStartTags(item As String) As String
' try to find a tag at the start of the line using StartsWith
If item.Trim().StartsWith("<") Then
' now search for the closing tag...
Dim lastLocation As Integer = item.IndexOf(">")
If lastLocation >= 0 Then
' remove the identified section, if it is a valid region
item = item.Substring((lastLocation + 1))
End If
End If
Return item
End Function 'StripStartTags
End Class 'EndsWithTest
[C#]
using System;
public class EndsWithTest {
public static void Main() {
// process a string that contains html tags
// this sample does not remove embedded tags (tags in the middle of a line)
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" };
Console.WriteLine("The following lists the items before the ends have been stripped:");
Console.WriteLine("-----------------------------------------------------------------");
// print out the initial array of strings
foreach ( string s in strSource )
Console.WriteLine( s );
Console.WriteLine();
Console.WriteLine("The following lists the items after the ends have been stripped:");
Console.WriteLine("----------------------------------------------------------------");
// print out the array of strings
foreach ( string s in strSource )
Console.WriteLine( StripStartTags( s ) );
}
private static string StripStartTags( string item ) {
// try to find a tag at the start of the line using StartsWith
if (item.Trim().StartsWith("<")) {
// now search for the closing tag...
int lastLocation = item.IndexOf( ">" );
// remove the identified section, if it is a valid region
if ( lastLocation >= 0 )
item = item.Substring( lastLocation + 1 );
}
return item;
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
String* StripStartTags(String* item)
{
// try to find a tag at the start of the line using StartsWith
if (item->Trim()->StartsWith(S"<")) {
// now search for the closing tag->->.
int lastLocation = item->IndexOf(S">");
// remove the identified section, if it is a valid region
if (lastLocation >= 0)
item = item->Substring(lastLocation + 1);
}
return item;
}
int main()
{
// process a string that contains html tags
// this sample does not remove embedded tags (tags in the middle of a line)
String* strSource[] = { S"<b>This is bold text</b>", S"<H1>This is large Text</H1>",
S"<b><i><font color=green>This has multiple tags</font></i></b>",
S"<b>This has <i>embedded</i> tags.</b>",
S"<This line simply begins with a lesser than symbol, it should not be modified" };
Console::WriteLine(S"The following lists the items before the ends have been stripped:");
Console::WriteLine(S"-----------------------------------------------------------------");
// print out the initial array of strings
IEnumerator* myEnum1 = strSource->GetEnumerator();
while (myEnum1->MoveNext()) {
String* s = __try_cast<String*>(myEnum1->Current);
Console::WriteLine(s);
}
Console::WriteLine();
Console::WriteLine(S"The following lists the items after the ends have been stripped:");
Console::WriteLine(S"----------------------------------------------------------------");
// print [Out] the* array of strings
IEnumerator* myEnum2 = strSource->GetEnumerator();
while (myEnum2->MoveNext()) {
String* s = __try_cast<String*>(myEnum2->Current);
Console::WriteLine(StripStartTags(s));
}
}
[JScript]
import System;
public class EndsWithTest {
public static function Main() : void {
// process a string that contains html tags
// this sample does not remove embedded tags (tags in the middle of a line)
var strSource : 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" ];
Console.WriteLine("The following lists the items before the ends have been stripped:");
Console.WriteLine("-----------------------------------------------------------------");
// print out the initial array of strings
for( var i : int in strSource )
Console.WriteLine( strSource[i] );
Console.WriteLine();
Console.WriteLine("The following lists the items after the ends have been stripped:");
Console.WriteLine("----------------------------------------------------------------");
// print out the array of strings
for ( i in strSource )
Console.WriteLine( StripStartTags( strSource[i] ) );
}
private static function StripStartTags( item : String ) : String {
// try to find a tag at the start of the line using StartsWith
if (item.Trim().StartsWith("<")) {
// now search for the closing tag...
var lastLocation : int = item.IndexOf( ">" );
// remove the identified section, if it is a valid region
if ( lastLocation >= 0 )
item = item.Substring( lastLocation + 1 );
}
return item;
}
}
EndsWithTest.Main();
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard