Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. comparing ascii values

comparing ascii values

Scheduled Pinned Locked Moved C#
csharphelp
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joindotnet
    wrote on last edited by
    #1

    I am using following code to verify whter enterd char is an alphabet. theAddress.Substring(i,1).ToLower < "a" The code runs perfectly in vb.net if I use Lcase but fails in c#.net. The error is: Error 1 Operator '<' cannot be applied to operands of type 'method group' and 'string' How else can I do that.The code is in a for loop. Plz help.I'd be obliged.

    S G 2 Replies Last reply
    0
    • J joindotnet

      I am using following code to verify whter enterd char is an alphabet. theAddress.Substring(i,1).ToLower < "a" The code runs perfectly in vb.net if I use Lcase but fails in c#.net. The error is: Error 1 Operator '<' cannot be applied to operands of type 'method group' and 'string' How else can I do that.The code is in a for loop. Plz help.I'd be obliged.

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      Well the error you are getting is because you missed the () from after the ToLower. It's a method call so you need the brackets. C# isn't like VB, you can't miss out the brackets if you don't pass any parameters. But your next problem is you can't use '<' to compare two strings. '<' only works on Char data types, not Strings. You could call .ToCharArray() on your string to get it as a character array and pick out the character from the array you want to look at, and then if you use single quotes (') rather than double quotes (") for the 'a' then that will be treated as a character too. But that would be a rather painful way of doing it. There are built in functions to do this kind of thing. Just do this:

      Char.IsLetter(yourString, i);

      Simon

      P G 2 Replies Last reply
      0
      • S Simon P Stevens

        Well the error you are getting is because you missed the () from after the ToLower. It's a method call so you need the brackets. C# isn't like VB, you can't miss out the brackets if you don't pass any parameters. But your next problem is you can't use '<' to compare two strings. '<' only works on Char data types, not Strings. You could call .ToCharArray() on your string to get it as a character array and pick out the character from the array you want to look at, and then if you use single quotes (') rather than double quotes (") for the 'a' then that will be treated as a character too. But that would be a rather painful way of doing it. There are built in functions to do this kind of thing. Just do this:

        Char.IsLetter(yourString, i);

        Simon

        P Offline
        P Offline
        Pedram Behroozi
        wrote on last edited by
        #3

        Hi Simon, I don't have VS in front of me but I think String has a Compare method. String.Compare, right? If so, He can use this method to compare his string instead of converting that into char array.

        HisString.Substring(i, 1).ToLower().Compare("a");

        I'm not sure if there's any Compare method in String. If there isn't, My apologize :) [Edit] Oh yes, I found that.

        HisString.Substring(i, 1).ToLower().Compare("a");
        String.Compare(HisString.Substring(i, 1).ToLower(), "a");

        Look here[^] [/Edit]

        Life is 5: 3 me, 1 you.

        modified on Tuesday, November 18, 2008 4:34 AM

        1 Reply Last reply
        0
        • S Simon P Stevens

          Well the error you are getting is because you missed the () from after the ToLower. It's a method call so you need the brackets. C# isn't like VB, you can't miss out the brackets if you don't pass any parameters. But your next problem is you can't use '<' to compare two strings. '<' only works on Char data types, not Strings. You could call .ToCharArray() on your string to get it as a character array and pick out the character from the array you want to look at, and then if you use single quotes (') rather than double quotes (") for the 'a' then that will be treated as a character too. But that would be a rather painful way of doing it. There are built in functions to do this kind of thing. Just do this:

          Char.IsLetter(yourString, i);

          Simon

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Simon Stevens wrote:

          But your next problem is you can't use '<' to compare two strings.

          Of course you can. As the string contains a single character, it will even work as expected, but it's not optimal.

          Despite everything, the person most likely to be fooling you next is yourself.

          S 1 Reply Last reply
          0
          • J joindotnet

            I am using following code to verify whter enterd char is an alphabet. theAddress.Substring(i,1).ToLower < "a" The code runs perfectly in vb.net if I use Lcase but fails in c#.net. The error is: Error 1 Operator '<' cannot be applied to operands of type 'method group' and 'string' How else can I do that.The code is in a for loop. Plz help.I'd be obliged.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            As Simon pointed out, the error comes from the missing parenthesis. However, you should not compare strings at all, as you only want to compare a single character at a time. Use the indexer of the string to get the character, use a static method in the char class to make it lower case, and compare it to a char literal: char.ToLower(theAddress[i]) < 'a'

            Despite everything, the person most likely to be fooling you next is yourself.

            P 1 Reply Last reply
            0
            • G Guffa

              As Simon pointed out, the error comes from the missing parenthesis. However, you should not compare strings at all, as you only want to compare a single character at a time. Use the indexer of the string to get the character, use a static method in the char class to make it lower case, and compare it to a char literal: char.ToLower(theAddress[i]) < 'a'

              Despite everything, the person most likely to be fooling you next is yourself.

              P Offline
              P Offline
              Pedram Behroozi
              wrote on last edited by
              #6

              Hello Guffa. Sorry for asking but is it faster than String.Compare(theAddress.Substring(i, 1).ToLower(), "a");? Thanks

              Life is 5: 3 me, 1 you.

              G 1 Reply Last reply
              0
              • G Guffa

                Simon Stevens wrote:

                But your next problem is you can't use '<' to compare two strings.

                Of course you can. As the string contains a single character, it will even work as expected, but it's not optimal.

                Despite everything, the person most likely to be fooling you next is yourself.

                S Offline
                S Offline
                Simon P Stevens
                wrote on last edited by
                #7

                It doesn't work for me. Try this:

                class Program
                {
                    static void Main(string\[\] args)
                    {
                        String s1 = "test1";
                        String s2 = "test2";
                        if (s1 < s2)
                        {
                        }
                    }
                }
                

                I get:

                Error: Operator '<' cannot be applied to operands of type 'string' and 'string'

                Simon

                G 1 Reply Last reply
                0
                • P Pedram Behroozi

                  Hello Guffa. Sorry for asking but is it faster than String.Compare(theAddress.Substring(i, 1).ToLower(), "a");? Thanks

                  Life is 5: 3 me, 1 you.

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Yes, it's faster. A character value is just a 16 bit value type, much more light-weight than creating string objects.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  1 Reply Last reply
                  0
                  • S Simon P Stevens

                    It doesn't work for me. Try this:

                    class Program
                    {
                        static void Main(string\[\] args)
                        {
                            String s1 = "test1";
                            String s2 = "test2";
                            if (s1 < s2)
                            {
                            }
                        }
                    }
                    

                    I get:

                    Error: Operator '<' cannot be applied to operands of type 'string' and 'string'

                    Simon

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    My apologies, you are correct. The string class overloads the == and != operators, but for some reason it does not overload the <, >, <= and >= operators.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    S 1 Reply Last reply
                    0
                    • G Guffa

                      My apologies, you are correct. The string class overloads the == and != operators, but for some reason it does not overload the <, >, <= and >= operators.

                      Despite everything, the person most likely to be fooling you next is yourself.

                      S Offline
                      S Offline
                      Simon P Stevens
                      wrote on last edited by
                      #10

                      No worries. :) I probably would have guessed the same as you. I only spotted it because I tried out his code.

                      Simon

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups