Different Ways to Split a String in C#

We will look at Different Ways to Split a String in C#. As developers, we may need to break strings into smaller substrings based on delimiters. C# includes the Split function as part of the String class.

Using of the String.Split() Function

The built-in String class is the simplest and most easy way to split a string in C#.Split() is a method. Based on a delimiter character or string, this method splits a string into an array of substrings. Here’s an example:

using System;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string strvalue = "Saving Account,Current Account, Fixed Deposit; Recurring Deposit";
            char[] delimeters = { ';', ',' };

            string[] chararray = strvalue.Split(delimeters);

            foreach(string c in chararray)
            {
                Console.WriteLine(c);
            }
          }
    }
}
Split String C#

Using the comma, and semicolon as the delimiter, will break the string myString into an array of three strings: Saving Account, Current Account, Fixed Deposit; Recurring Deposit

StringSplitOptions

The StringSplitOptions enumeration can be used as a parameter for the Split method in C#. When splitting a string, we can specify whether to include or exclude empty substrings from the resulting array.

There are three StringSplitOptions enumerations:

None0When dividing strings, use the default options.
RemoveEmptyEntries1Remove from the result any array members that contain an empty string.

When RemoveEmptyEntries and TrimEntries are used together, substrings that only include white-space characters are deleted from the result.
TrimEntries2Remove all white-space characters from the result’s substrings. Only.NET 5 and later versions support this field.
When RemoveEmptyEntries and TrimEntries are used together, substrings that only include white-space characters are deleted from the result.
 using System;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string strvalue = "Saving Account,Current Account & Fixed Deposit; Recurring Deposit";
            char[] delimeters = {';', ',','&'};

            string[] chararray = strvalue.Split(delimeters,StringSplitOptions.RemoveEmptyEntries);

            foreach (string c in chararray)
            {
                Console.WriteLine(c);
            }
       }
    }
}
Split String C#

Try it Yourself

If you apply both the options RemoveEmptyEntries and TrimEntries, any substrings that only contain whitespaces will be removed from the resulting array. The TrimEntries option will remove any leading or trailing whitespaces from substrings before checking if they are empty, while the RemoveEmptyEntries option will eliminate any empty substrings from the final result.

Splitting a String into New Lines

We can use the Split method in combination with the Environment to split a string into new lines. Property owned by NewLine.

This method allows us to split the string at each occurrence of a newline character sequence, which, depending on the platform, could be a single newline or a combination of a carriage return and a newline.

 using System;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string strvalue = "Saving Account \nCurrent Account  \nFixed Deposit \nRecurring Deposit ";
            
           string[] chararray = strvalue.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            foreach (string c in chararray)
            {
                Console.WriteLine(c);
            }


        }
    }
}
Split String C#

basically, we define a multi-line string that consists of four lines of text separated by newline characters. Then, we use the Split method, with the delimiter set to Environment.NewLine, to split the string at each occurrence of a newline character sequence. As a result, we obtain an array with four elements, where each element represents a single line from the original string.

More Related Post How to Identify If a String is a Number in C#

Conclusion

Splitting a string in C# is a common task that may be achieved using several kinds of methods, including the built-in String.Split(), and Stringsplitoptions, are all methods. Select the approach that best meets your needs based on the delimiter, regular expression pattern, or position of the substrings to be extracted.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments