Languages
[Edit]
EN

C# / .NET - split string by space character

0 points
Created by:
Zeeshan-Peel
730

In this article, we would like to show you how to split string by space character in C#.

Quick solution:

String originalString = "How to split string by space?";

// simple split by space char
String[] split1 = originalString.split(" ");

// split by whitespace regex - " " 
String[] split2 = Regex.Split( originalString, @" ");

// split by whitespace regex once or more - " +"
String[] split3 = Regex.Split( originalString, @" +");

Note: 

The regular expression " +" is equivalent to "[ ]+".

 

1. C# simple split by space char

using System;

public class Program
{
    public static void Main(string[] args)
    {
        string example = "How to split string by space?";
        String[] split = example.Split(" ");

        Console.WriteLine("# Original string: ");
        Console.WriteLine(example);
        Console.WriteLine();

        Console.WriteLine("# Split by space string: ");
        int counter = 1;
        foreach (String str in split)
        {
            Console.WriteLine(counter + " - " + str);
            counter++;
        }
    }
}

Output:

# Original string:
How to split string by space?

# Split by space string:
1 - How
2 - to
3 - split
4 - string
5 - by
6 - space?

2. C# split by space regex

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main(string[] args)
    {
        string originalString = "How to split string by space?";
        string[] splittedString = Regex.Split(originalString, @" ");

        Console.WriteLine("# Original string: ");
        Console.WriteLine( originalString );
        Console.WriteLine();

        Console.WriteLine("# Split by space string: ");
        int counter = 1;
        foreach (string stringElement in splittedString)
        {
            Console.WriteLine(counter + " - " + stringElement);
            counter++;
        }
    }
}

Output:

# Original string:
How to split string by space?

# Split by space string:
1 - How
2 - to
3 - split
4 - string
5 - by
6 - space?

3. C# split by space regex once or more

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main(string[] args)
    {
        string originalString = "How       to split string by  space?";
        string[] splittedString = Regex.Split(originalString, @" +");

        Console.WriteLine("# Original string: ");
        Console.WriteLine( originalString );
        Console.WriteLine();

        Console.WriteLine("# Split by space string: ");
        int counter = 1;
        foreach (string stringElement in splittedString)
        {
            Console.WriteLine(counter + " - " + stringElement);
            counter++;
        }
    }
}

Output:

# Original string: 
How       to split string by  space?

# Split by space string: 
1 - How
2 - to
3 - split
4 - string
5 - by
6 - space?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - split string by space character

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join