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 Punkjim420 · Jan 24, 2013 at 10:29 AM · javascriptstringsearchgeneric list

How do you check if a string has already been removed from a generic list?

I use javascript.
How do you check if a string has already been removed from a generic list? i have an if statement:

     if(Master.ether == 0){
     
         Master.itembag.Remove("Ether");
         
     }

and it keeps trying to remove the string "Ether" from the list once it is 0. i need it to happen once only. i know i can make a toggle type tidbit of code with.. but is there a method for searching generic list for specific strings?

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

1 Reply

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

Answer by asafsitner · Jan 24, 2013 at 10:46 AM

You're looking for **Contains**.

As in

 if(Master.ether == 0 && Master.itembag.Contains("Ether"))
 {
    Master.itembag.Remove("Ether");
 }

or

 if(Master.ether == 0)
 {
     if(Master.itembag.Contains("Ether"))
     {
         Master.itembag.Remove("Ether");
     }
 }


EDIT

It's worth noting that List.Contains performs a linear search and thus has complexity of O(n) where n is the number of elements in the list. This means that as the list grows longer, searching it becomes slower. It shouldn't be an issue as long as your list is relatively small though.

On the other hand, if you want you could implement your inventory using a Dictionary or Hashtable, both of which you can do ContainsKey search that is always O(1), meaning they can instantly find the key regardless of the collection size. Obviously this is far more optimized, but potentially more cluttered to work with.
For our specific inventory case though I believe it will be the better approach all around, as you could specify the dictionary key as the item name and the value as the number of items the inventory contains.

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 Punkjim420 · Jan 24, 2013 at 11:06 AM 0
Share

Thanks a tons thats exactly what i wanted to know. Also, my list is definately going to be at most around 10 items i size. So it should be a quick search im guessing. However, If i ever create a larger inventory I will definately read up on Dictionaries and Hashtables. Thanks again.

avatar image asafsitner · Jan 24, 2013 at 11:09 AM 0
Share

Glad I could help :)

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

10 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

Related Questions

string searching question 1 Answer

search is not a member of string 2 Answers

string.search() in Unity Javascript? 1 Answer

Character at index of string? Finding which character is at an index like in Javascript charAt 1 Answer

get all text between two chars (js) 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