problem comparing Strings using compareTo() as it never evaluates to 0 even when the two strings are equal
I'm storing a list of English words provided in txt format, where each word is separated by a line , to a List < string > with the following code
public TextAsset text;
List<string> words = new List<string>();
void Start () {
words = (text.text.Split("\n"[0])).ToList<string>();
}
and then using binary search on the List and string.compareTo() I'm trying to find weather a specific word exists or not
public bool binarySearch(string word) {
int min = 0;
int max = words.Count;
int mid = (min + max) / 2;
int value = word.CompareTo(words[mid]);
while (min<=max){
if (value == 0) {
return true;
}
else if (value > 0)
{
min = mid + 1;
mid = (min + max) / 2;
}
else
{
max = mid - 1;
mid = (min + max) / 2;
}
value = word.CompareTo(words[mid]);
}
return false;
but I'm running into a problem where I am able to locate the correct index of the matching word but for some reason, which I think is how the strings are stored into the List, the compareTo() never returns 0, can somebody figure out what is going on?
Your answer
![](https://koobas.hobune.stream/wayback/20220612074858im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
change variable from textAsset in dictionary 1 Answer
Pick a random string from a string array C# 1 Answer
Find First Parentheses in String 2 Answers
How to round to 2 decimals with format? (123456 > 123K > 123.45K) (C#) 0 Answers
Help int.Parse "Input String was not in the correct format" PROBLEM 0 Answers