Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read only 20 characters from a string and compare to other string?

I am working on hashing. I am hashing a phrase and I can only use 20 characters of the phrase.

How can I read only 20 characters of a string?

How can I compare strings if they are the same?

like image 596
NewHelpNeeder Avatar asked Dec 01 '11 09:12

NewHelpNeeder


1 Answers

this compares the first 20 characters of string a and b

if (String.Compare(a, 0, b, 0, 20) == 0)
{
    // strings are equal
}

for culture specific comparison rules you can use this overload, that accepts a StringComparison enum:

if (String.Compare(a, 0, b, 0, 20, StringComparison.CurrentCultureIgnoreCase) == 0)
{
    // case insensitive equal
}
like image 75
thumbmunkeys Avatar answered Oct 18 '22 05:10

thumbmunkeys