Exercise - Discover Split() and Join()
- 10 minutes
As you continue your development work for a logistics company, you begin building a series of small applications. These applications work together to take data from one partner's system, modify it, and then pass it to an internal system in the required format.
To perform data transformation, you need to accept incoming data as a string, parse it into smaller data elements, then manipulate it to match different format required. How can you parse the string data into smaller data elements?
String data type's Array methods
The variables of type string
have many built-in methods that convert a single string into either an array of smaller strings, or an array of individual characters.
When you process data from other computer systems, sometimes it formats or encodes in a way that's not useful for your purposes. In these cases, you use the string
data type's Array methods to parse a string into an array.
Use the ToCharArray()
to reverse a string
Delete or use the line comment operator
//
to comment out all of the code from the previous exercises.Update your code in the Visual Studio Code Editor as follows:
string value = "abc123"; char[] valueArray = value.ToCharArray();
In this example, the ToCharArray()
method is used to create an array of char
, where each element of the array represents one character of the original string.
Reverse, then combine the char array into a new string
Next, the order of the characters in the array is reversed, and then the Write
method is used to combine them back into a single output.
Update your code in the Visual Studio Code Editor as follows:
string value = "abc123"; char[] valueArray = value.ToCharArray(); Array.Reverse(valueArray); string result = new string(valueArray); Console.WriteLine(result);
The expression
new string(valueArray)
creates a new empty instance of theSystem.String
class (which is the same as thestring
data type in C#) and passes in the char array as a constructor.Note
What is the
new
keyword? How is theSystem.String
class related to thestring
data type in C#? What is a constructor? All great questions that unfortunately are out of scope for this module. You are recommended to keep learning about the .NET Class Library as well as classes and objects in C# to fully understand what is going on behind the scenes with this expression of code. For now, use a search engine and Microsoft Documentation to find examples you can use in situations like this where you know you want to perform a conversion but are not sure how to do it using C#.On the Visual Studio Code File menu, select Save.
The Program.cs file must be saved before building or running the code.
In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click TestProject, and then select Open in Integrated Terminal.
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
At the Terminal command prompt, to run your code, type dotnet run and then press Enter.
Note
If you see a message saying "Couldn't find a project to run", ensure that the Terminal command prompt displays the expected TestProject folder location. For example:
C:\Users\someuser\Desktop\csharpprojects\TestProject>
You should see the following output:
321cba
Combine all of the chars into a new comma-separated-value string using Join()
In some cases, you might need to separate each element of the character array using a comma, which is a common practice when working with data represented as ASCII text. To do that, you comment out the line of code you added in Step 2 and use the String
class' Join()
method, passing in the char you want to delimit each segment (the comma) and the array itself.
Update your code in the Visual Studio Code Editor as follows:
string value = "abc123"; char[] valueArray = value.ToCharArray(); Array.Reverse(valueArray); // string result = new string(valueArray); string result = String.Join(",", valueArray); Console.WriteLine(result);
Save your code file, and then use Visual Studio Code to run your code.
You should see the following output:
3,2,1,c,b,a
Split()
the comma-separated-value string into an array of strings
To complete the code, the Split()
method is used. This method is designed for variables of type string
and creates an array of strings.
Use the Visual Studio Code Editor to add the following lines of code at the bottom of the file:
string[] items = result.Split(','); foreach (string item in items) { Console.WriteLine(item); }
Take a minute to review the previous code.
The comma is supplied to
.Split()
as the delimiter to split one long string into smaller strings. The code then uses aforeach
loop to iterate through each element of the newly created array of strings,items
.Check that your code now appears as follows:
string value = "abc123"; char[] valueArray = value.ToCharArray(); Array.Reverse(valueArray); // string result = new string(valueArray); string result = String.Join(",", valueArray); Console.WriteLine(result); string[] items = result.Split(','); foreach (string item in items) { Console.WriteLine(item); }
Save your code file, and then use Visual Studio Code to run your code.
When you run the code, you see the following output:
3,2,1,c,b,a 3 2 1 c b a
The
items
array created usingstring[] items = result.Split(',');
is used in theforeach
loop and displays the individual characters from the originalstring
contained in thevalue
variable.
Recap
Here are a few key points to remember when working with strings and arrays:
- To create an array, use methods like
ToCharArray()
andSplit()
- To turn the array back into a single string, use methods like
Join()
, or create a new string passing in an array ofchar