StartsWith. This C# method tests the first part of strings. We use it to test the first characters in a string against another string. It is possible to test many strings with the foreach-loop.
With EndsWith, we test the last characters. These two methods provide an easy way to test and match common strings such as URLs. They are used throughout C# programs.
Example. We can use StartsWith and EndsWith to check the beginning and end of strings. These methods can be used for efficient testing of strings that have known starts or ends.
using System;
// The input string.
string input = "abcdef";
if (input.StartsWith("abc"))
{
Console.WriteLine("STARTSWITH: ABC");
}
if (input.EndsWith("def"))
{
Console.WriteLine("ENDSWITH: DEF");
}STARTSWITH: ABC
ENDSWITH: DEF
Foreach, StartsWith. Next, we see that foreach can be used with StartsWith. Here we test elements in a string array against the input string, returning true if there is a match.
using System;
// The input string.
string input = "http://site.com/test.html";
// The possible matches.
string[] urls = new string[]
{
"http://www.site.com",
"http://site.com"
};
// Loop through each possible match.
foreach (string value in urls)
{
if (input.StartsWith(value))
{
// Will match second possibility.
Console.WriteLine(value);
return;
}
}http://site.com
EndsWith. This tests the last parts of strings. It finds strings that have a certain ending sequence of characters. EndsWith is a simple way to test for ending substrings.
Detail The EndsWith method, like its counterpart the StartsWith method, has 3 overloaded method signatures.
Here The first example shows the simplest and first overload, which receives one parameter.
Tip To use EndsWith, you must pass the string you want to check the ending with as the argument.
Next An input string is tested for 3 ends. We detect the ending extension of the URL.
using System;
// The input string.
string input = "http://site.com";
// Test these endings.
string[] array = new string[]
{
".net",
".com",
".org"
};
// Loop through and test each string.
foreach (string value in array)
{
if (input.EndsWith(value))
{
Console.WriteLine(value);
return;
}
}.com
Overloads. StartsWith accepts a second parameter—a StringComparison enumerated constant. You can use String Comparison to specify case-insensitive matching—try OrdinalIgnoreCase.
Char test, performance. Consider the StartsWith method: we can duplicate its functionality with individual char tests. In benchmarks, testing chars usually is faster.
Summary. With StartsWith we can test the first characters of strings. It returns a bool telling us whether or not the starts match. EndsWith, meanwhile, tests the end of strings.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2023 (rewrite).