Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C# string Split



C# Split() handles splitting upon given string and character delimiters. It returns an array of String containing the substrings delimited by the given System.Char array.

If your String contains “dd-mm-yy”, split on the “-” character to get an array of: “dd” “mm” “yy”.

The String Split method ignores any element of separator whose value is null or the empty string (“”).

Syntax :

string[] string.split(string[] separator)

Parameters:

  1. Separator – the given delimiter

Returns:

  1. An array of Strings delimited by one or more characters in the separator.
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string str = null;
            string[] strArr = null;
            int count = 0;
            str = "CSharp split test";
            char[] splitchar = { ' ' };
            strArr = str.Split(splitchar);
            for (count = 0; count <= strArr.Length - 1; count++)
            {
                MessageBox.Show(strArr[count]);
            }
        }
    }
}

Output:

CSharp
split
test

C# String Split Example

How to split strings using regular expressions

The Regular Expressions Split() methods are almost similar to the String.Split() method, except that Regex.Split() method splits the string at a delimiter determined by a Regular Expression instead of a set of characters.

When using Regular Expressions, you should use the following namespace in your project.

using System.Text.RegularExpressions;
string str = "one\n   \ntwo\n   \nthree\n   \n   \nfour";
string[] result = Regex.Split(str, "\n\\s*");
for (int i = 0; i < result.Length; i++)
    MessageBox.Show(result[i]);

 Output:

one
two
three
four

c# String Split by multiple characters delimiter

We can split a string by multiple character delimiter using String.split() method.

string input = "one)(two)(three)(four)(five";
string[] result = input.Split(new string[] { ")(" }, StringSplitOptions.None);
foreach (string s in result)
    MessageBox.Show(s);

Output:

one
two
three
four
five

Using Regular Expressions for multiple characters

C# String Split by multiple characters delimiter using Regular Expressions.

string input = "one)(two)(three)(four)(five";
string[] result = Regex.Split(input, @"\)\(");
foreach (string s in result)
    MessageBox.Show(s);

Output:

one
two
three
four
five

C# String split New Line

You can split a string between a new line or carriage return using the delimiter “\r\n”.

C# String split Carriage Return.

string test = "One\nTwo\r\nThree\nFour\n";
string[] result = test.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
    MessageBox.Show(s);

Output:

one
two
three
four

Environment.NewLine

Also, you can use the Environment.NewLine to remove the new line from a string

string test = "One\r\nTwo\r\nThree\r\nFour";
string[] result = test.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string s in result)
    MessageBox.Show(s);

Output:

one
two
three
four

How to split() a delimited string to a List < String >

You can retrieve the result of a String splt() method to a C# List. The following program converts the String Array to a List.

C# Convert List to String

string s = "This is a sentence.";
IList<string> list = new List<string>(s.Split(new string[] { " is " }, StringSplitOptions.None));
foreach (string element in list)
{
	MessageBox.Show(element);
}

C# String split White spaces.

StringSplitOptions.RemoveEmptyEntries guarantees the return value does not include array elements that contain an empty string. The following C# program shows how to remove all white spaces from string using StringSplitOptions.RemoveEmptyEntries.

using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myStrA = "one two   three    four     five";
            string[] result = myStrA.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in result)
                MessageBox.Show(s);

        }
    }
}

When you execute this C# program, you will get the result removing all white spaces from the string.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More