String.StartsWith Method (String)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Determines whether the beginning of this instance matches the specified string.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.String
The string to compare.
Return Value
Type: System.Booleantrue if value matches the beginning of this string; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | value is null. |
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.
The following code example demonstrates how you can use the StartsWith method.
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; } }