locked
Convert.tochar RRS feed

  • Question

  • Hi

    What the right syntax for convert.tochar ?

    if i write                          

     foreach (char letter in str.ToCharArray())
       {
            switch (letter)
              {
                    case Convert.ToChar("a"):
                            sum += 1;

       

     the code line    case Convert.ToChar("a"):  get an error

    Thanks

    Wednesday, October 7, 2020 4:01 PM

Answers

  • Try this

    string foo = "Hello Adam and welcome";
    int count = 0;
    foreach (char c in foo)
    {
        if (c == 'a')
        {
            count += 1;
        }
    }
    
    Console.WriteLine(count);

    Switch

    string foo = "Hello Adam and welcome";
    int count = 0;
    foreach (char c in foo)
    {
        switch (c)
        {
            case 'a':
                count += 1;
                break;
            case 'h':
                count += 1;
                break;
        }
    }
    
    Console.WriteLine(count);

    Or

    string foo = "Hello Adam and welcome";
    
    Console.WriteLine(foo.Count(x => char.ToUpperInvariant(x) == char.ToUpperInvariant('a')));
    Console.WriteLine(foo.Count(x => char.ToUpperInvariant(x) == char.ToUpperInvariant('h')));
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    • Edited by KareninstructorMVP Wednesday, October 7, 2020 5:08 PM
    • Marked as answer by Shay_w Wednesday, October 7, 2020 9:33 PM
    Wednesday, October 7, 2020 5:00 PM

All replies

  • Try this:

       foreach( char letter in str )

       {

          switch( letter )

          {

          case 'a':

             sum += 1;

             break;

          . . .

    Wednesday, October 7, 2020 4:57 PM
  • Try this

    string foo = "Hello Adam and welcome";
    int count = 0;
    foreach (char c in foo)
    {
        if (c == 'a')
        {
            count += 1;
        }
    }
    
    Console.WriteLine(count);

    Switch

    string foo = "Hello Adam and welcome";
    int count = 0;
    foreach (char c in foo)
    {
        switch (c)
        {
            case 'a':
                count += 1;
                break;
            case 'h':
                count += 1;
                break;
        }
    }
    
    Console.WriteLine(count);

    Or

    string foo = "Hello Adam and welcome";
    
    Console.WriteLine(foo.Count(x => char.ToUpperInvariant(x) == char.ToUpperInvariant('a')));
    Console.WriteLine(foo.Count(x => char.ToUpperInvariant(x) == char.ToUpperInvariant('h')));
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    • Edited by KareninstructorMVP Wednesday, October 7, 2020 5:08 PM
    • Marked as answer by Shay_w Wednesday, October 7, 2020 9:33 PM
    Wednesday, October 7, 2020 5:00 PM