+92 332 4229 857 99ProjectIdeas@Gmail.com

String Compare Function (C#.net)




Compares the two specified string objects and returns integer that indicates their relative position in the sort order.






For example:

     string strOne = "Osama";
            string strTwo = "Maverick";
            MessageBox.Show(string.Compare(strOne, strTwo).ToString());
    
     Output:
            1 (because strOne is smaller than strTwo)

If you write above code like this, then:
     
     string strOne = "Osama";
            string strTwo = "Maverick";
            MessageBox.Show(string.Compare(strTwo, strOne).ToString());
    
    Output:
            -1 (because strTwo isn’t smaller than strOne)


Another Example:
             
       string strOne = "Osama";
       string strTwo = "Maverick";
       MessageBox.Show(strOne.CompareTo(strTwo).ToString());

     Output:
            1 (because strOne is smaller than strTwo)

If code is like this, then:
     
       MessageBox.Show(strTwo.CompareTo(strOne).ToString());
    
    Output:
           -1 (because strTwo isn’t smaller than strOne)

0 comments: