Have a read of these
Arrays Tutorial (C#)[
^]
String.Split Method (System)[
^]
String.Substring Method (Int32, Int32) (System)[
^]
string[] displayStringArrays = null;
This means that displayStringArrays is an array of strings, however it is simply being defined at this point, it doesn't actually contain an array of strings just yet. The other thing of note is that at this point we don't know the size of the array either, we just know that this variable can now contain an array of strings in the future.
displayStringArrays = text.Split('\n').ToArray();
First off the code above doesn't need the ToArray
displayStringArrays = text.Split('\n');
Split converts a single string into an array of strings using the character you supply as the separator. So if text was "abcabcabcdef" then Split('c') on that text would result in an array like this
displayStringArrays[0] = "ab";
displayStringArrays[1] = "ab";
displayStringArrays[2] = "ab";
displayStringArrays[3] = "def";
So when you Split('\n'), \n is the code for new line, you are taking a chunk of text that has multiple lines and creating an array where each element in that array is a line. So if the text was;
how now
brown cow
you'd have an array like
displayStringArrays[0] = "how now";
displayStringArrays[1] = "brown cow";
foreach (var item in displayStringArrays)
That line will cause the code in the { } block to loop once for each item in your array, so the first iteration "item" will be displayStringArrays[0], the second iteration will be displayStringArrays[1] and so on.
Basically what the code is doing is splitting the text in the file into an array of lines, and processing each line at a time with that line being in "item".
if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)
That's not good code. The "||" means "or", so if the statement to the left or it or the statement to the right is true the "if" evaluates as true. The statement to the right (listText.Text == item) is straightforward, it means if the text in that textbox is the same as "item".
The code to the left is comparing the text box with the substring of "0, length-1" of "item". That means starting at character 0 (the first) and going to the length of item minus 1, ie the second but last character. So if "item" is "Hello" then that substring is
"Hell"
The length of Hello is 5, so starting at the first character and taking the 4 characters you get "Hell"
So it item is "Hello" and listText.Text is "Hell" + <any character> then that will resolve to true. So if listText is any of these;
Hella
Hellb
Hellc
Helld
Helle
then
listText.Text == item.Substring(0, item.Length - 1)
will resolve to true. I assume that logic was done for a reason and I am going to further assume it is to capture either a trailing space, so a trailing carriage return. As we can't see your data we don't know, but that is my guess. If that Is right then there are better ways of handling that, eg changing the Split to this
displayStringArrays = text.Split(new char[]{'\r','\n'}, StringSplitOptions.RemoveEmptyEntries);
If the code is there to ignore trailing spaces then you can use Trim()
if (listText.Text.Trim() == item.Trim())
As it is the code has introduced a bug that is going to give unintended results. Eg the line might be
"Mend"
and your compare text might be "Men" and that is going to register a match when it isn't.