Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Nov 06, 2013 at 10:38 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
5
Question by ExpiredIndexCard · Apr 25, 2013 at 11:06 PM · c#arraylistsexists

How to check if a value exists in an array (C#)

I am making a level generation algorithm. I have all my objects stored in an array. These objects are determined by numbers. I need to check whether the number "0" exists or not. How do I do this? Thanks

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

3 Replies

  • Sort: 
avatar image
8
Best Answer

Answer by Johnnemann · Apr 26, 2013 at 12:27 AM

If you're using an actual Array, you can use Array.Find() or Exists() to look for the matching member: http://msdn.microsoft.com/en-us/library/d9hy2xwa.aspx or http://msdn.microsoft.com/en-us/library/yw84x8be.aspx

However, if you use a List instead, you can use List.Contains(), which has a bit easier syntax. http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

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 markwelbar · Jul 24, 2013 at 06:59 AM

you can check simple like the following :

C# Array full source code

         string stringToCheck = "GHI";
         string[] stringArray = { "ABC", "DEF", "GHI", "JKL" };
         foreach (string x in stringArray)
         {
             if (x.Equals (stringToCheck))
             {
                 MessageBox.Show("Find the string ..." + x);
             }
         }


Mark

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 sharpcoders · Nov 06, 2013 at 10:36 AM

Its pretty simple, use this

 string[] stringArray = { "text1", "text2", "text3", "text4" };
 string value = "text3";
 int pos = Array.IndexOf(stringArray, value);
 if (pos > -1)
 {
     return true;
 }
 else
 {
     return 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 firemyst · Nov 10, 2014 at 12:32 AM 0
Share

The fastest way to search an array is doing a binary search:

Array.BinarySearch

This website benchmarks other methods as well, but the binary search always came out on top.

Follow this Question

Answers Answers and Comments

14 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 avatar image avatar image

Related Questions

Affect every object in array. 1 Answer

Help Sorting large class full of variables.,Creating and array of arrays of arrays of variables with different data types. 1 Answer

how can i check if index exists? 2 Answers

How to modify array values? 1 Answer

How to access individual class elements, that has an array, that is in a list for c# 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