3
Answers

String compare result

harish reddy

harish reddy

7y
151
1
string str1 = "COMPUTER";
string str2 = "computer";
string str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case:");
result = string.Compare(str1, 2, str2, 2, 2, true);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
 
in above example, in this line result = string.Compare(str1, 2, str2, 2, 2, true);
why "true" is there? Also when i complie i am getting result as 0, result=0; 
pls let me know. 
Answers (3)
2
Hiten Pandya

Hiten Pandya

NA 318 1.5k 7y
Hii Harish , the True word in the syntax indicates that it will ignore the case  it means it does not matter wheather it is "COMPUTER" or "computer" there is no difference between that and because of that you are getting 0 as a result.
 
Here if you change that true to false then you will get result 1 and greater than as per your program.
 
"The CSharp String Compare function compares two strings lexicographically.The comparison is based on the Unicode value of each character in the string."
 
 
0
Ajeesh

Ajeesh

NA 349 1.6k 7y
You are using overload of String.Compare method. You can take a look at this article for more details
https://msdn.microsoft.com/en-us/library/1e058ek8(v=vs.110).aspx
 
 
0
Dharmraj Thakur

Dharmraj Thakur

NA 4.1k 61.7k 7y
I agree with @Hiten Pandya