I have a string list where i add my Tags.
A Tag cotains a Line, Source and Target.
Tag example: L02_FTxx10_STxx5
L02 -> Line
FTxx10 -> Source
STxx5 -> Target
I split every tag into an array and then i add the Line/Source/Target in lists.
The problem is that the Source is not always at the same place, so i can't use a static index of the array.
Tag example: L02_word_word_FTxx10_word_STxx5
The Source has always 'xx' in it. So i want to find the index of the first string that contains 'xx'.
Can i use 'Array.IndexOf'? if yes, how?
Or is there another way?
my code:
foreach (string s in TagLijst)
{
int index = -1;
string[] tempArr = s.Split('_');
index = Array.IndexOf(???);
if (!Line.Contains(tempArr[0])) { Line.Add(tempArr[0]); }
if (!Source.Contains(tempArr[index])) { Source.Add(tempArr[index]); }
if (!Target.Contains(tempArr.Last())) { Target.Add(tempArr.Last()); }
Array.Clear(tempArr);
}
Unfortunately, as you are storing you tags within a string and not a more complex data structure (such as a custom class), you will need to test the individual string segments for the substring (in your case, "xx"). However, there are ways to make it more efficient.
Drawing from your question, the tag begins with Line, contains zero or more words, lists Source, contains zero or more words, and finishes with Target. Line, Source, and Target are all definites (they must appear in each tag). At this point, we can already rule out the first and last elements from the Source problem.
(At this point, I am being cautions, but you can modify my answer to make function better.) Source begins with some characters, contains the string "xx", and concludes with some more characters. This identifying characteristic allows us to iterate over the array and get the correct index.
From your example, I assumed that there would be more
_word_s from in the first section, so I sent the search from back to front. If it is the other way, or you don't know, you can reverse the search. Once the element in the array containing the substring "xx" is identified, the loop terminates and the current index is stored. After that, you can use it as you wish.Some further notes:
forloop into one line withfor (; index > 0 && !arr[index].Contains("xx"); index--);if you want a smaller footprint, but I'd recommend against it for readability..Length) before what it contains can shorten the search by some computer cycles. This is because the string's length is stored as a property and only requires a quick lookup, while.Containsis a function and must actually test the characters of the element, requiring multiple calculations. But this should only be added if there are a large number of those words; otherwise the.Lengthlookups will total as more expensive than just starting with.Contains..StartsWithinstead of.Contains. This is because.StartsWithreturnsfalseas soon as the condition fails, while.Containsmust check the entire string (minus the length of the testing substring) before it can returnfalse.