Cannot use operand '<' on type string and string
Im making a game where Im doing a binary search through an array of strings i made to see if another string matches a string in the array. Only problem with the binary search is the Unity say i cannot compare type 'string' and 'string'. Am I doing something wrong here? Heres the code for the function:`public bool RealWord(string a){
public bool RealWord(string a){
int low = 0;
int high = 172820;
int count = 0;
while (count < 19) {
int currentindex = (high + low) / 2;
string currentword = mylist [currentindex]
if (b == currentword) {
return true;
} else if (a > currentword) {
low = currentindex;
} else if (a < currentword) {
high = currentindex;
}
++count;
}
return false;
}
You can't use the operators on stings use the String.Compare method this there are several overloads check the string documentation too here . Or you could use a Hashset might be faster from a binary search, quite discussed topic search around if performance is an issue. (Note there is an Array.BinarySearch method too but i am not familiar with it). Cheers hope that helps.
Your answer
Follow this Question
Related Questions
Storing input to array 1 Answer
Wher is the problem? My string isn't behaving... 1 Answer
String matching C# 1 Answer
Trying to spawn enemies on only one path 1 Answer
Building tycoon game, need help storing variables into an array. 1 Answer