Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Searching a character in a string in C#

You can search a letter in a string by converting the string to character array.

Working example:

If the user wants to search whether the character 'e' is present or not in a string "My name is Forget Code"
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ForgetCode
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string text = "My name is Forget Code";
  13. char[] textarray = text.ToCharArray();
  14.  
  15. foreach (char c in textarray)
  16. {
  17. if (c.Equals('e'))
  18. {
  19. Console.WriteLine("The letter is present");
  20. break;
  21. }
  22.  
  23. }
  24. }
  25. }
  26. }

Output:
The letter is present