- Home /
String comparison, can't use false result as string.
I have to make a string comparison and use the boolean result as a string stored in a table. I use this table to make a sort-related research (i can't store true or false as boolean so i use string). But i can't get false result. when the function should return false the execution seems to stop and nothing happens, my table dont get anything ... i need help :)
does_meuble_is_in_research.Insert(tab_index, test_recherche_field(curTab[2].ToString()).ToString());
public bool test_recherche_field(String meuble_name_to_test){//s_recherche => champ de saisie
bool b_ok = false;
mssg_debug = mssg_debug+" NOM=> "+meuble_name_to_test;
for(int index_letter=0; index_letter<meuble_name_to_test.Length; index_letter++){//
if(s_recherche == meuble_name_to_test.Substring(index_letter, s_recherche.Length)){
b_ok = true;
break;
}
}
return b_ok;
}
Answer by jahroy · Jan 18, 2012 at 11:40 PM
I only see one place where you make a string comparison, in your for loop.
I don't use C# personally, so my first question is this: Can you use the == operator to compare strings?
You might have to use the String.Equals() method.
Either way, I think your code will throw an Exception before it can possibly return false.
I read your code like this:
If sRecherche is a substring of meubleNameToTest, set bOK to true and break out of the loop forever. Return "true".
If sRecherche is NOT a substring of meubleNameToTest, your code will throw an IndexOutOfRange exception (or something like that). It can't ever determine that rRecherche is NOT in meubleNameToTest, because it will always throw an exception before it gets to the end of the loop.
What will happen when sRecherche is not found in meubleNameToTest?? If we assume that meubleNameToTest is ten characters long and sRecherche is 3 characters long, you will run into trouble when indexLetter is greater than 7! (String.Substring will be asked to return characters 8, 9, and 10 and ten is out of range!)
Maybe this is the cause of your problem.
Long story short, you should change your for loop to this:
int patternLen = sRecherche.Length; int loopMax = meubleStringToTest.Length - patternLen;
for ( int i = 0; i < loopMax; i ++ ) {
string searchString = meubleStringToTest.Substring(i, patternLen);
/* nous l'avons trouvé! */
if ( loopMax.Equals(searchString) ) {
return "true";
}
}
/ pas trouvé /
return "false";
Note: I've changed the names of your variables to conform to .Net naming conventions.
Otherwise Unity Answers goes berzerk trying to parse all the underscores! Also, it's much easier to read code if you use its naming conventions. Your variable names would be great in a php program, but they're hard on my eyes here.
Answer by DaveA · Jan 18, 2012 at 10:40 PM
How is s_recherche defined? In any case, right, it won't convert bool to string automatically, but you can do something like this:
string s = boolval == true ? "true" : "false";
s_recherche is an input field comparing when the user presses the "search" button
Answer by Kalu · Jan 18, 2012 at 10:54 PM
does_meuble_is_in_research.Insert(tab_index, test_recherche_field(curTab[2] as string));
public string test_recherche_field(String meuble_name_to_test){//s_recherche => champ de saisie
bool b_ok = false;
mssg_debug = mssg_debug+" NOM=> "+meuble_name_to_test;
for(int index_letter=0; index_letter<meuble_name_to_test.Length; index_letter++){//
if(s_recherche == meuble_name_to_test.Substring(index_letter, s_recherche.Length)){
b_ok = true;
break;
}
}
string s = b_ok == true ? "true" : "false";
return s;
}
but no change :s ..
Answer by Kalu · Jan 19, 2012 at 03:36 PM
Thank you very much, I did not have to change my code but against you identified a big problem when a 3-character string entered trying to compare the last two letters of reference chain more one space: I just re-write => -s_recherche.Length => for(int index_letter=0; index_letter<(meuble_name_to_test.Length-s_recherche.Length); index_letter++){
public string test_recherche_field(String meuble_name_to_test){//s_recherche => champ de saisie
bool b_ok = false;
mssg_debug = mssg_debug+" NOM=> "+meuble_name_to_test;
for(int index_letter=0; index_letter<(meuble_name_to_test.Length-s_recherche.Length); index_letter++){
if(s_recherche == meuble_name_to_test.Substring(index_letter, s_recherche.Length)){
b_ok = true;
break;
}
}
string s = b_ok == true ? "true" : "false";
return s;
}
Thank you very much and sorry for my variables, I study the web this year and all my teachers tell me to banish the upper case -__-
Yeah, that for loop was definitely not going to work.
No worries on the variables.
Every language has a different standard for variable names.
Java, javascript, and C# use camel case (mixed lowercase and uppercase).
PHP, C, and SQL tend to use lowercase letters and underscores.
I finally re-write it again :) , work perfectly and test all word separate than other (i juste post it, if someone else have the same need)
public string test_recherche_field(String meuble_name_to_test){//s_recherche => champ de saisie
bool b_ok = false;
meuble_name_to_test = meuble_name_to_test.ToLower();
string[] s_sub_ref_meuble = meuble_name_to_test.Split(' ');
s_recherche = s_recherche.ToLower();
string[] s_sub_ref_research = s_recherche.Split(' ');
foreach (string meubl_word in s_sub_ref_meuble){
mssg_debug = mssg_debug+" meuble_word => "+meubl_word;
foreach (string research_word in s_sub_ref_research){
mssg_debug = mssg_debug+" resarch_word => "+research_word;
if(research_word.Length > meubl_word.Length){ break; }
if(research_word == meubl_word.Substring(0, research_word.Length)){
b_ok = true;
break;
}
}
}
string s = b_ok == true ? "true" : "false";
return s;
}
Your answer
Follow this Question
Related Questions
C# ArrayList match to string? 1 Answer
bool[string] = true; array possible? 2 Answers
C# String Array Has Missing or Incorrect Keycode Strings 2 Answers
Turn based games turn manager is spazzing out 2 Answers
C# Incrementing a String Array 1 Answer