In C#, the StartsWith()
method is used to check whether a given string starts with a specified substring. It returns a Boolean value indicating whether the string starts with the specified substring or not.
Here’s the syntax of the StartsWith()
method:
public bool StartsWith(string value)
The StartsWith()
method is a member of the System.String
class. It takes a single parameter, value
, which represents the substring to check for at the beginning of the string. It returns true
if the string starts with the specified substring, and false
otherwise.
Here’s an example that demonstrates the usage of StartsWith()
method:
string text = "Hello, World!"; bool startsWithHello = text.StartsWith("Hello"); Console.WriteLine(startsWithHello); // Output: True bool startsWithHi = text.StartsWith("Hi"); Console.WriteLine(startsWithHi); // Output: False
In the above example, the StartsWith()
method is used to check whether the text
string starts with “Hello” and “Hi”. The first call to StartsWith()
returns true
because the string starts with “Hello”, while the second call returns false
because the string does not start with “Hi”.
Parameter:
The StartsWith()
method in C# takes a single parameter:
value
(string): This parameter represents the substring that you want to check if it is present at the beginning of the string. It is of typestring
.
Here’s an example that demonstrates the usage of the value
parameter:
string text = "OpenAI is amazing!"; bool startsWithOpen = text.StartsWith("Open"); Console.WriteLine(startsWithOpen); // Output: True
In the above example, the StartsWith()
method is called with the parameter "Open"
. It checks whether the text
string starts with the substring “Open” and returns true
because the string does indeed start with “Open”.
Return:
The StartsWith()
method in C# returns a Boolean value indicating whether the string starts with the specified substring or not. It has the following return value:
true
if the string starts with the specified substring.false
if the string does not start with the specified substring.
Here’s an example that demonstrates the return value of the StartsWith()
method:
string text = "Hello, World!"; bool startsWithHello = text.StartsWith("Hello"); Console.WriteLine(startsWithHello); // Output: True bool startsWithHi = text.StartsWith("Hi"); Console.WriteLine(startsWithHi); // Output: False
In the above example, the StartsWith()
method is called twice with different substrings. The first call with the substring “Hello” returns true
because the string starts with “Hello”. The second call with the substring “Hi” returns false
because the string does not start with “Hi”.
C# String StartsWith() Method Example:
Certainly! Here’s a complete example that demonstrates the usage of the StartsWith()
method in C#:
using System; class Program { static void Main() { string text = "OpenAI is amazing!"; bool startsWithOpen = text.StartsWith("Open"); Console.WriteLine($"Starts with 'Open': {startsWithOpen}"); // Output: True bool startsWithAI = text.StartsWith("AI"); Console.WriteLine($"Starts with 'AI': {startsWithAI}"); // Output: False bool startsWithAmazing = text.StartsWith("Amazing"); Console.WriteLine($"Starts with 'Amazing': {startsWithAmazing}"); // Output: False } }
In this example, we have a string text
containing the text “OpenAI is amazing!”. We use the StartsWith()
method to check if the string starts with different substrings: “Open”, “AI”, and “Amazing”. The results are displayed using Console.WriteLine()
.
When you run this program, you’ll get the following output:
Starts with 'Open': True Starts with 'AI': False Starts with 'Amazing': False
As you can see, the first call to StartsWith()
returns true
because the string starts with “Open”. The second and third calls return false
because the string does not start with “AI” or “Amazing”.