String.StartsWith Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines whether the beginning of this instance matches the specified string.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function StartsWith ( _
    value As String _
) As Boolean
public bool StartsWith(
    string value
)

Parameters

Return Value

Type: System.Boolean
true if value matches the beginning of this string; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

Remarks

This method compares value to the substring at the beginning of this instance that is the same length as value, and returns an indication whether they are equal. To be equal, value must be an empty string (String.Empty), a reference to this same instance, or match the beginning of this instance.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

Notes to Callers

Starting in Silverlight 4, the behavior of the String.StartsWith(String) method has changed. In Silverlight 4, it performs a case-sensitive and culture-sensitive comparison using the current culture to determine whether the current instance begins with value. This conforms to the behavior of the String.StartsWith(String) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.StartsWith(String) performs an ordinal comparison using the current culture. If the common language runtime determines that a Silverlight-based application was compiled against Silverlight 2 or Silverlight 3, it performs an ordinal comparison; otherwise, it performs a culture-sensitive comparison.

Examples

The following code example demonstrates how you can use the StartsWith method.


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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)

      outputBlock.Text &= "The following lists the items before the tags have been stripped:" & vbCrLf
      outputBlock.Text &= "-----------------------------------------------------------------" & vbCrLf

      ' print out the initial array of strings
      Dim s As String
      For Each s In strSource
         outputBlock.Text &= s & vbCrLf
      Next s

      outputBlock.Text &= vbCrLf

      outputBlock.Text &= "The following lists the items after the tags have been stripped:" & vbCrLf
      outputBlock.Text &= "----------------------------------------------------------------" & vbCrLf

      ' print out the array of strings
      For Each s In strSource
         outputBlock.Text &= StripStartTags(s) & vbCrLf
      Next s

   End Sub 'Main

   Private Shared Function StripStartTags(ByVal 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
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // 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" };

      outputBlock.Text += "The following lists the items before the tags have been stripped:" + "\n";
      outputBlock.Text += "-----------------------------------------------------------------" + "\n";

      // print out the initial array of strings
      foreach (string s in strSource)
         outputBlock.Text += s + "\n";

      outputBlock.Text += "\n";

      outputBlock.Text += "The following lists the items after the tags have been stripped:" + "\n";
      outputBlock.Text += "----------------------------------------------------------------" + "\n";

      // print out the array of strings
      foreach (string s in strSource)
         outputBlock.Text += StripStartTags(s) + "\n";
   }

   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;
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.