...

/

Working with Text: String Manipulation Operations

Working with Text: String Manipulation Operations

Learn about splitting a string, getting the part of the string, and checking a string for content.

Let’s see the string manipulation operations like splitting a string, getting part of it, and checking a string for content.

Splitting a string

Sometimes, we need to split some text wherever there is a character, such as a comma:

Step 1: Add statements to define a single string variable containing comma-separated city names, then use the Split method and specify that we want to treat commas as the separator, and then enumerate the returned array of string values, as shown in the following code:

Press + to interact
string cities = "Paris,Tehran,Chennai,Sydney,New York,Medellín";
string[] citiesArray = cities.Split(',');
WriteLine($"There are {citiesArray.Length} items in the array:");
foreach (string item in citiesArray)
{
WriteLine(item);
}

Step 2: ...