Solved

Need help understanding 'Split'.  C#

Posted on 2016-07-15
4
51 Views
Last Modified: 2016-07-15
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?
0
Comment
Question by:huerita37
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41713232
the [0] is the indexer. You probably mean:

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

Open in new window

0
 

Author Comment

by:huerita37
ID: 41713239
I didn't write the code and when I was trying to read it I was getting confused.
0
 

Author Closing Comment

by:huerita37
ID: 41713251
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 ';'.
0
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41713265
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

1

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reading an excel file vb.net 2 47
FTP recursive download files 5 28
C# LINQ query question 11 33
MessageBox Appear behind a form in Runtime 1 32
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question