Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I have generated a String Array named inputstringarray from a string named inputstring by splitting the string at intervals where the character "'" appears.

string[] inputstringarray = inputstring.Split('\'');


Say i have used a string like
"'44'Is'GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType '1'"


So, this will be parted to a string array like

inputstringarray[0]=""
inputstringarray[1]=44
inputstringarray[2]=Is
inputstringarray[3]=GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType 
inputstringarray[4]=1
inputstringarray[5]=""


How do i find the index of the Array element having the string "Is" in the String Array ?

If i am using a different string, then obviously the index will change. The IndexOf function available to String is not available for String Array.

How can i find the index of the Array element having a particular string value in the String Array ?
Posted
Updated 18-Oct-13 1:12am
v2
Comments
Nelek 18-Oct-13 7:37am    
Can't you use a foreach or an old-fashioned for, compare the string and save the index when you find it? you could use "mid" or other String functions, depending on what you need.
Divakar Raj M 18-Oct-13 7:45am    
Yes, but i was trying for way to avoid "For" since my String Array is large and I don't want to compromise on efficiency

Hi,
try this.
C#
int TextIndex = Array.FindIndex(inputstringarray, m => m == "Is");//replace "Is" with your other desired text

Hope it helps you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Oct-13 10:22am    
Sure, a 5.
—SA
Ok, we have Array static member, we have for cycle... what are we missing? Right, Linq expressions! :)

You could try the following:

int[] indexes = inputstringarray.Select((str, index) => str.Equals("Is", StringComparison.InvariantCulture) ? index : -1).Where(iElement => iElement >= 0).ToArray();


This gives you the array with indexes of all "Is" string inclusions.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 18-Oct-13 10:23am    
5ed.
—SA
I'd like to suggest you use the form of the String.Split method that removes "empty entries:"
// defining the char[] used to Split in advance 
// can save memory if you are using it repeatedly
private char[] splitCharAry = new char[] { '\'' };

string str = "'44'Is'GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType '1'";

string[] sAry = str.Split(splitCharAry, StringSplitOptions.RemoveEmptyEntries);
Once you have your "cleaned up" string[], .NET hands you a variety of ways to find an item, some of which you've already seen in the other responses to your question: IndexOf, Array.Find, Array.FindIndex, etc. In later versions of .NET those functions are actually being transformed for you into Linq calls ... you are just "spared" having to write out the Types.

If I needed to search frequently, I might write a function like this for convenience:
C#
// This method is an O(n) operation, where n is the Length of array.
private int findArrayStringElement(string searchStr, bool UseContains, string[] strAry)
{
    return (UseContains) 
      ?
        Array.FindIndex(strAry, element => element.Contains(searchStr))
        :
        Array.FindIndex(strAry, element => element == searchStr);
}
So I could search either for something that absolutely matched the string I wanted to find, or for an item that "contained" the string I was trying to find. All the above methods return the integer value #-1 if there is not match.

An important thing to keep in mind is that searching an Array that is not sorted for an item is always going to (potentially) require iterating over all the Array elements until the item is found: using Linq expressions are not going to be any faster than using a for-loop, or foreach, iterator !

In computer-science terms, finding an item in an unsorted list/array is going to be an O(#n) operation, where #n is the Length of array/list. However, if the array/list is sorted, then you can, potentially (depending on factors like the length of the list/array), get much faster performance.

In .NET using the BinarySearch facility on a sorted list/array can get you something like O(log #n) results where #n is the length of the array/list.
C#
Array.Sort(sAry);

// test
int sResult = Array.BinarySearch(sAry, "Is");
Since "nothing comes for free," it's logical to consider "how much" a sort "costs." Here: [^] Microsoft describes how the .NET BinarySearch facility works, and how, for different size search targets it uses different methods for the search (see the 'Remarks section for details). Technically, the type of Sort MS implemented here is an Introsort [^], a "hybrid sort."

So the question I think you should consider is: "to Sort; or not to Sort ?"

If you are going to search the same result of splitting a string many times, then, yes, I think you will probably benefit from sorting. If you are dealing with splitting a string whose value changes frequently, then perhaps you'll get no benefit from sorting.

For an array with a few entries: well, I think you'll have to experiment and see what the benefits are.
 
Share this answer
 
I don't think its the best way, but you can do this:

C#
string src = "Is";
int x = -1;

for (int i = 0; i < inputstringarray.Length; i++) { if (inputstringarray[i] == src) { x = i; } }





if (x > -1)
{
    MessageBox.Show(x.ToString());
}
 
Share this answer
 
v2
See below the code to get index from string array

C#
string inputstring = "'44'Is'GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType '1'";
      string[] inputstringarray = inputstring.Split('\'');
      int index = Array.IndexOf(inputstringarray, "Is"); 


Main point you are finding IndexOf keyword from Array with one string value
 
Share this answer
 

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