comparing ascii values
-
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.
-
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.
Well the error you are getting is because you missed the
()
from after theToLower
. 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 onChar
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
-
Well the error you are getting is because you missed the
()
from after theToLower
. 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 onChar
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
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");Life is 5: 3 me, 1 you.
modified on Tuesday, November 18, 2008 4:34 AM
-
Well the error you are getting is because you missed the
()
from after theToLower
. 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 onChar
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
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.
-
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.
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.
-
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.
Hello Guffa. Sorry for asking but is it faster than
String.Compare(theAddress.Substring(i, 1).ToLower(), "a");
? ThanksLife is 5: 3 me, 1 you.
-
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.
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
-
Hello Guffa. Sorry for asking but is it faster than
String.Compare(theAddress.Substring(i, 1).ToLower(), "a");
? ThanksLife is 5: 3 me, 1 you.
-
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
-
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.
No worries. :) I probably would have guessed the same as you. I only spotted it because I tried out his code.
Simon