Search through Strings with String Split in C#

string split c#We use strings to communicate. We cannot go one day without using a string or two in our lives. Strings are nothing more than sequences of characters including letters, numbers, punctuation marks, and everything in between. Given how much we depend on strings, it only makes sense that we find them in every computer programming language including C#. In our programs, we use strings to gather data from the user or to display a message on the screen. We even use strings as identifiers when retrieving and storing data in databases.

Almost all computer user interactions are done via strings. If you ever used a search engine, your search query is a string. If you logged into a website using a username and a password, your username and password are strings. While we use strings to communicate with each other and our computers, we must understand that computers see strings much differently that we do.

To us, strings hold special meanings. To us, strings are words, sentences, or paragraphs. We can even store whole books into a single string. In each case, we understand as a unit. On the other hand, to a computer, a string is nothing more than an array of characters. Computers do not understand the concepts of words, sentences, or paragraphs. If we need our computers to process words as words, we need to write the code that does it ourselves. The method String.Split() in C# lets us do just that.

Learn how to use strings in C# at Udemy.com

The String Split Method

Computers don’t understand the concepts of words or sentences. Fortunately, computers see strings as character arrays, and this is how we get our computers to understand and manipulate words and phrases. We do this by adding sentinels to our strings, and then calling the strings’ Split() function.

Sentinels mark the end of one word or phrase and the beginning of the next. We can use any character we want for this role, but we must make sure we don’t need to include the character in our desired words and phrases. Common conventions dictate that we use the same punctuation marks we use for this role in our everyday language such as spaces and commas. These sentinels are important because they tell the string split function how to split up our strings into words and phrases.

String Split is an inherent function of all strings in C#. As strings are considered object classes in C#, we use Split like we do with any other object’s method. Split takes a character array as an argument, and it returns a string array. The basic syntax looks like this:

string[] str = MyString.Split(separator);

Separator is an optional parameter and it where we put the character sentinel we choose earlier. If we do not include a sentinel, C# will default it to a space (‘ ‘). Separator can either be a string containing our sentinel character or characters or a string literal. In either case, the sentinel cannot be an empty string (‘’) or null.

string[] ls = langs.Split(',');

Split converts your string into a string array based on your separator. As you can see, C# stores this new string array into a variable of your choice, leaving the original string alone. It works by reading your string until it hits a separator character. It then cuts off and stores the characters it just read into an index of your array before moving on to the next word in your string. It does this until it reaches the end of the string. The separator itself is ignored. It just makes were each string in the array begins and ends. This is why you should never use a character you need process later as a separator.

Learn about other ways to manipulate strings in C#

Using String Split in Your C# Code

The String Split method produces a string array. It is this array you use when you need to do further processing on your string. The string array is just an ordinary string away with the words and phrases of your string stored in each index in the order they were in your string. For instance, check out the following code

string content = "I love splitting string in C#";

string[] str = content.Split();

foreach (string word in str)

{

Console.WriteLine(word);

}

The code outputs as:

I

love

splitting

strings

in

C#

Learn how to use arrays in C# at Udemy.com

Searching Within a String Using String Split in C#

You use this string array like any array as well. This is where the power of split comes in.

Most programmers use split to investigate user generated input. For instance, your program might have a search function where users can search through a database of help files. It might have a form where the user can input their full name and address. In either case, you can use the Split function to split these strings into useful information.

In these cases, you can read each word in the array just by using the array name with the right index. This is great if you know the sequence ahead of time and the string isn’t that long. For longer strings, I would recommend that you use a C# loop to process the string array index by index. You can use any loop, but keep it to either use the foreach loop as I used in the example above, or the for loop. You use a for loop to run through your string array like this:

for (int i = 0; i < str.Length(); i++)

{

Console.WriteLine(str[i]);

}

Using Multiple Separators with Split

As each element in the produced string array is a string itself, you can call its Split method as well.  This lets you use additional separators if your sting input needs further processing. Since Split() only excepts a single character, you can use this cascading technique to give your programs to process complicated stings such as internet addresses and query strings.

string querystring = “website=udemy.com&language=CSharp”;

string[] query = querystring.Split(‘&’);

foreach (string element in query)

{

String[] item = element.Split(‘=’);

Console.WriteLine(“The value of “ + item[0] + “ is “ + item[1]);

}