I'd like to suggest you use the form of the String.Split method that removes "empty entries:"
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:
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.
Array.Sort(sAry);
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.