0

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);
            } 
4
  • Yes, you have to check every single letter. No skipping. Commented Jan 21, 2024 at 19:51
  • Help us by making the question clearer - share some sample input and the expected output Commented Jan 21, 2024 at 19:55
  • Is it possible to have a string with more than 2 consecutive x's? Commented Jan 21, 2024 at 20:19
  • Only the source and target string has 'xx' in it. The target is always the last string in the array. i edited my question and made clearer i think. Commented Jan 21, 2024 at 20:36

1 Answer 1

0

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.

string[] arr = s.Split("_");
int index = arr.Length - 2; // skip last item since that is Target
for (; index > 0; index--)
    if (arr[index].Contains("xx"))
        break;

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:

  • You can compress the for loop into one line with for (; index > 0 && !arr[index].Contains("xx"); index--); if you want a smaller footprint, but I'd recommend against it for readability.
  • If many of the strings you are to test contain many extraneous words, and you know Source must always be a specific length (from your example, 6), testing the element's length (.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 .Contains is 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 .Length lookups will total as more expensive than just starting with .Contains.
  • If Source always starts with certain characters (in your example, "FTxx") that include the "xx", use .StartsWith instead of .Contains. This is because .StartsWith returns false as soon as the condition fails, while .Contains must check the entire string (minus the length of the testing substring) before it can return false.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.