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 :slight_smile:

    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;
	}

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";

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.

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 …

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 -__-