C#: Check whether a given string starts with 'C#' or not
Starts with 'C#'
Write a C# Sharp program to check whether a given string starts with 'C#' or not.
Visual Presentation:

Sample Solution:
C# Sharp Code:
using System;
// Namespace declaration
namespace exercises
{
// Class declaration
class Program
{
// Main method - entry point of the program
static void Main(string[] args)
{
// Calling the 'test' method with different strings and displaying the returned values
Console.WriteLine(test("C# Sharp")); // Output: True
Console.WriteLine(test("C#")); // Output: True
Console.WriteLine(test("C++")); // Output: False
Console.ReadLine(); // Keeping the console window open
}
// Method to test conditions based on the given string
public static bool test(string str)
{
// Check if the length of the string is less than 3 and it equals "C#"
// or if the string starts with "C#" and the third character is a space ' '
return (str.Length < 3 && str.Equals("C#")) || (str.StartsWith("C#") && str[2] == ' ');
}
}
}
Sample Output:
True True False
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C# program to check if a string starts with either 'C#', 'VB#', or 'F#'.
- Write a C# program to determine if a string starts with 'C#' and ends with '.NET'.
- Write a C# program that returns true only if the string starts with 'C#' but does not contain 'Java'.
- Write a C# program to count how many strings in a list start with 'C#'.
Go to:
PREV : First Three Characters Front and Back.
NEXT : Temperature Comparison.
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.