Link to home
Start Free TrialLog in
Avatar of Jacque Scott
Jacque ScottFlag for United States of America

asked on

Need help understanding 'Split'. C#

I need help to make sure I am understanding the below code correctly.

FYI: textBox1 is an input box.  The user can search for anything they want.

textBox1.Text.Split(';')[0]

Open in new window


Is the 'Split' looking for ';' as a separator that the user inputted to search for multiple items in the textBox1?  If so, then does the [0] mean only the first item listed is being searched for?

Here is more of the code:

for (int i = 0; i < textBox1.Text.Split(';')[0].Length; i++)
                    {
                        if (textBox1.Text.Split(';')[0][i] == '.')
                        {
                            cnt++;
                        }
                    }

Open in new window


Can someone let me know if I am reading this code correctly?
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacque Scott

ASKER

I didn't write the code and when I was trying to read it I was getting confused.
The [0] is an indexer.  Not sure why but the original programmer allowed the user to input a search for multiple items but never searched for anything after the first ';'.
If you're ever curious, take your code and write outputs to see what it does.  Like this:

string test = "Hey; what is the problem?  Did you know; One period.  Two periods.  Not three!";
            int cnt = 0;
            Console.WriteLine(test.Split(';').Length.ToString());
            for (int i = 0; i < test.Split(';').Length; i++)
            {
                
                if (test.Split(';')[0][i] == '.')
                {
                    cnt++;
                }
                Console.WriteLine(test.Split(';')[0][i].ToString());
            }
            Console.WriteLine(test.Split(';')[0].ToString());
            Console.WriteLine(cnt.ToString());
            Console.Read();

Open in new window


You can see what exactly the code is doing.  In this case, yes they never iterate paste the first block of ';'.

You need to use a nested for(i++) in order to iterate through the first item and the second set.

Like this:

string test = "Hey; what is the problem?  Did you know; One period.  Two periods.  Not three!";
            int cnt = 0;
            Console.WriteLine(test.Split(';').Length.ToString());
            for (int i = 0; i < test.Split(';').Length; i++)
            {
                for (int j = 0; j < test.Split(';')[i].Length; j++)
                {
                    if (test.Split(';')[i][j] == '.')
                    {
                        cnt++;
                    }
                    Console.WriteLine(test.Split(';')[i][j].ToString());
                }
                
            }
            
            Console.WriteLine(cnt.ToString() + " periods found.");
            Console.Read();

Open in new window