Click here to Skip to main content
15,891,810 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a string array
and i want to check , if index in array exist or not
for exp, my array can be

ckh[0]="111"
ckh[2]="222"


or

VB
ckh[1]="345"
ckh[2]="222"


i want to check if string array has 0 , 1 , 2 index or not
Posted
Comments
joginder-banger 13-Jan-14 4:30am    
what's you try ??
joginder-banger 13-Jan-14 4:30am    
array.count();
maulikshah1990 13-Jan-14 4:31am    
i use Getvalue , elementat..but that gives error ..
maulikshah1990 13-Jan-14 4:33am    
i used try and catch and get error as
{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}
Cenarjun 13-Jan-14 4:45am    
use array.contains()

Try this..



C#
string[] ckh = { "111", "222" };

        string value = "111";
        int index =  ckh.ToList().IndexOf(value);
 
Share this answer
 
Comments
maulikshah1990 13-Jan-14 4:45am    
I first search for index because i dont know value.they are dynamic
i have just put example above
Karthik_Mahalingam 13-Jan-14 4:46am    
ok, what is your issue >?
provide more details..
maulikshah1990 13-Jan-14 4:51am    
I have values in string array ...
i have string array as

ckh[0]=null
ckh[1]="22d"
ckh[2]="aa"
or it can be

ckh[0]=null
ckh[1]=null
ckh[2]="aqs"

and i run for loop and want to check if 0 or 1 or 2 exist , then get value inside that

thats it............
Karthik_Mahalingam 13-Jan-14 4:55am    
try like this
string[] ckh = new string[3] ;

ckh[0] = null;
ckh[1] = "22d";
ckh[2] = "aa";

for (int i = 0; i < ckh.Length; i++)
{
if (ckh[i] != null)
{
string value = ckh[i];
}
}
C#
string [] arr = {"One","Two","Three"};
              var target = "One";
              var results = Array.FindAll(arr, s => s.Equals(target));


OR
C#
string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
    if (x.Contains(stringToCheck))
    {
        // Process...
    }
}


Give it a try.This may help.If not ask here.
 
Share this answer
 
v2
 
Share this answer
 
v2
Comments
maulikshah1990 13-Jan-14 5:00am    
i got the answer by using ElementAtOrDefault
Gandalf_TheWhite 13-Jan-14 5:02am    
Great! Thumbs Up!
When you create an Array of String means that is automatically assigned null values to each index position. Now you need to check null exist or not on any position like:


C#
string []ckh = new string[10];
            ckh[0]="111";
            ckh[2] = "222";
            for(int i=0;i<ckh.Length;i++)
            {
                if(ckh[i]!=null)
                {
                    Response.Write(String.Format("Index {0} Value is {1}",i,ckh[i]));

                }
                else
                {
                     Response.Write(String.Format("Index {0} does not exist",i));

                }
            }
 
Share this answer
 
you could do without running a loop

C#
protected void Button1_Click(object sender, EventArgs e)
    {

       string[] myArray=  {"A","B","C"};
       string myValue;

        //GET 4th ELEMENT
        try
        {
            myValue = myArray[4];
        }
        catch (Exception)
        {
            myValue = "does not exist";
        }


    }
 
Share this answer
 
i got the answer by using ElementAtOrDefault
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Mar-14 0:49am    
Not an answer.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900