Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kalu · Jan 18, 2012 at 10:38 PM · arraystringbooleancomparison

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;
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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";
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kalu · Jan 18, 2012 at 10:43 PM 0
Share

s_recherche is an input field comparing when the user presses the "search" button

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Jan 19, 2012 at 04:28 PM 0
Share

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.

avatar image Kalu · Jan 19, 2012 at 04:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges