String.StartsWith Method
Determines whether the beginning of this instance matches the specified String.
[Visual Basic] Public Function StartsWith( _ ByVal value As String _ ) As Boolean [C#] public bool StartsWith( string value ); [C++] public: bool StartsWith( String* value ); [JScript] public function StartsWith( value : String ) : Boolean;
Parameters
- value
- The String to seek.
Return Value
true if value matches the beginning of this string or is Empty; otherwise false.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | value is a null reference (Nothing in Visual Basic). |
Remarks
This method makes a comparison at the beginning of the string, determines whether it matches this current instance, and returns a Boolean representation of their relationship. The specified string must match the prefix or be an empty string (that is, equals Empty).
This method performs a word (case-sensitive and culture-sensitive) search using the current culture.
Example
The following code example demonstrates how you can use the StartsWith method.
[Visual Basic] 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();
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard