• 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 ArcIo · Dec 18, 2012 at 12:15 AM · arraystringfindcompare

How do I find a String in an Array?

humanReadableWord is declared at the top of the file and works. I can print it and I get a string. I'm able to print the array with allWords just fine. I can pull from the array. I can print this.humanReadableWord with no problem but I can't compair them for some reason... I think.

 var humanReadableWord : String;
 
 function Update(){
     var selectedLetters = GameObject.Find("SelectedLetters").GetComponent(SelectedLetters);
     var currentWordInProgress = selectedLetters.selectedLettersArray;
     this.humanReadableWord = (currentWordInProgress).Join("");
 }
 
 function ValidateWord(){
     var linkToDictionary = GameObject.Find("Dictionary").GetComponent(Dictionary);
     var allWords = linkToDictionary.dictionaryWords;
     var currentWord = ""+this.humanReadableWord;
 
     for (var i = 0; i < allWords.length; i++) {
 //   print(this.humanReadableWord);  <--Works
        if(allWords[i] == currentWord){
          print("testing!!");
        }
     }  
 }

I have a call to the function below this that I also know is working...

Comment
Add comment · Show 3
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 DaveA · Dec 18, 2012 at 12:19 AM 0
Share

I don't see where humanReadableWord is declared

avatar image DaveA · Dec 18, 2012 at 12:20 AM 0
Share

You might also like this: http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.80).aspx

avatar image ArcIo · Dec 18, 2012 at 01:02 AM 0
Share

Original post edited. I'm still working on this and I'm wondering if I'm missing something here.

" if(allWords[i] == currentWord){ ""

2 Replies

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

Answer by Eric5h5 · Dec 18, 2012 at 01:30 AM

Use Array.Contains; you don't need to loop through the array yourself. Also I don't know what "var currentWord = ""+this.humanReadableWord;" is for since it's not doing anything...you can just use the humanReadableWord variable instead.

Comment
Add comment · Show 6 · 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 ArcIo · Dec 18, 2012 at 01:37 AM 0
Share

I tried using the Array.Contians but I always get 'Contains' is not a member of 'Array'.

avatar image Eric5h5 · Dec 18, 2012 at 01:42 AM 0
Share

You're not using the JS Array class are you? Always use built-in arrays or generic List ins$$anonymous$$d.

avatar image ArcIo · Dec 18, 2012 at 01:44 AM 0
Share

I'm very new to programming but very dedicated. All day I've been reading about arrays and what types are what. This is what i did to create my Array. I get the results from it but I can't search it. I tried using the builtin but it acts like it doesn't recognize the builtin.

This is what I have.

var dictionaryWords = new Array(); var dictionaryTextFile : TextAsset;

function Start () {

var returnChar = "\n"[0]; this.dictionaryWords = this.dictionaryTextFile.text.Split(returnChar); }

avatar image Eric5h5 · Dec 18, 2012 at 02:17 AM 0
Share

O$$anonymous$$, you should do this ins$$anonymous$$d:

 var dictionaryWords : String[];
avatar image ArcIo · Dec 18, 2012 at 03:03 AM 0
Share

Was never able to get the Application to be ok with Array.Contains. Unity gripes every time. So I would still love to resolve that but now I have a new issue and maybe it's too big for this thread. So I might start a new one.

It turns out that the Array that I'm building is tacking on an extra space at the end of each word. So for instance when I run length on the word in the array and the word I'm using, I get the following.

word in array "aah" // length = 4 word user is typing "aah" // length = 3

That's why the match never happens but now I'm blown away as to why it's saying my string is one letter too long. I patched a band-aid on it by chopping it one char shorter and its working fine but it's a kluge to be sure...

Thoughts?

Show more comments
avatar image
0

Answer by blkbear · Dec 18, 2012 at 02:30 AM

first try printing out the variable currentWord. And if the log is what you want then maybe the array value at i is not a match. You could also try using === which implies strictly equal

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 People are following this question.

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

Related Questions

Check if specific object exist in list or array, Best Practices? 2 Answers

How can I detecting whether certain characters appear in any strings in my array of strings? 1 Answer

Convert Array to String 3 Answers

Reverse a String? 0 Answers

[SOLVED] First array slot blocking second array slot 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges