.NET Framework 類庫
String.StartsWith 方法 (String)
請參見 示例
確定此實例的開頭是否與指定的字符串匹配。
命名空間:System
程序集:mscorlib(在 mscorlib.dll 中)
語法
C#
public bool StartsWith (
string value
)
參數
value
要比較的 String。
返回值
如果 value 與此字符串的開頭匹配,則爲 true;否則爲 false。
異常
異常類型 條件
ArgumentNullException
value 爲空引用(在 Visual Basic 中爲 Nothing)。
備註
此方法將 value 與位於此實例開頭、與 value 長度相同的子字符串進行比較,並返回它們是否相等的指示。若要相等,value 必須是空字符串 (Empty)、對此同一實例的引用,或者必須與此實例的開頭匹配。
此方法使用當前區域性執行單詞(區分大小寫和區域性)比較。
示例
下面的代碼示例說明了如何使用 StartsWith 方法。
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 tags 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 tags 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 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( "<" ) )
{
// 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;
}
int main()
{
// process a string that contains html tags
// this sample does not remove embedded tags (tags in the middle of a line)
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"};
Console::WriteLine( "The following lists the items before the tags have been stripped:" );
Console::WriteLine( "-----------------------------------------------------------------" );
// print out the initial array of strings
IEnumerator^ myEnum1 = strSource->GetEnumerator();
while ( myEnum1->MoveNext() )
{
String^ s = safe_cast<String^>(myEnum1->Current);
Console::WriteLine( s );
}
Console::WriteLine();
Console::WriteLine( "The following lists the items after the tags have been stripped:" );
Console::WriteLine( "----------------------------------------------------------------" );
// print [Out] the* array of strings
IEnumerator^ myEnum2 = strSource->GetEnumerator();
while ( myEnum2->MoveNext() )
{
String^ s = safe_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 tags 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 tags 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 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 並不是對每個平臺的所有版本都提供支持。有關受支持版本的列表,請參見系統要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
請參見
參考
String 類
String 成員
System 命名空間
EndsWith