C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Split String Into Array

Splitting a string into an array is a common operation in programming. In C#, you can use the Split method provided by the string class to split a string into an array of substrings based on specified delimiters. This tutorial will cover the following topics related to splitting a string into an array in C#:

  • Basic string splitting
  • Splitting with multiple delimiters
  • Splitting with StringSplitOptions

Let's begin!

  • Basic string splitting

To split a string into an array of substrings based on a single delimiter, you can use the Split method:

string input = "one,two,three";
char delimiter = ',';

string[] result = input.Split(delimiter);

foreach (string item in result)
{
    Console.WriteLine(item);
}

This will output:

one
two
three
  • Splitting with multiple delimiters

To split a string into an array of substrings based on multiple delimiters, pass an array of delimiter characters to the Split method:

string input = "one,two;three:four";
char[] delimiters = { ',', ';', ':' };

string[] result = input.Split(delimiters);

foreach (string item in result)
{
    Console.WriteLine(item);
}

This will output:

one
two
three
four
  • Splitting with StringSplitOptions

By default, the Split method includes empty substrings in the result when there are consecutive delimiters in the input string. You can control this behavior by using the StringSplitOptions enumeration.

To remove empty substrings from the result, pass StringSplitOptions.RemoveEmptyEntries as a parameter:

string input = "one,,two;;three::four";
char[] delimiters = { ',', ';', ':' };

string[] result = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

foreach (string item in result)
{
    Console.WriteLine(item);
}

This will output:

one
two
three
four

That's it! You've now learned how to split a string into an array in C# using the Split method, including basic string splitting, splitting with multiple delimiters, and splitting with StringSplitOptions. The Split method is useful for parsing and processing text data that is delimited by specific characters.

  1. How to use the Split method in C#

    The Split method in C# is used to split a string into an array of substrings based on a specified delimiter.

    string input = "apple,orange,banana";
    string[] fruits = input.Split(',');
    
    // Result: ["apple", "orange", "banana"]
    
  2. C# split string by delimiter

    Split a string based on a single delimiter.

    string input = "one;two;three";
    string[] parts = input.Split(';');
    
    // Result: ["one", "two", "three"]
    
  3. Splitting a string into words in C#

    Use whitespace as a delimiter to split a string into words.

    string sentence = "C# is fun!";
    string[] words = sentence.Split(' ');
    
    // Result: ["C#", "is", "fun!"]
    
  4. C# split string by multiple delimiters

    Split a string using multiple delimiters.

    string input = "apple,orange;banana";
    string[] fruits = input.Split(',', ';');
    
    // Result: ["apple", "orange", "banana"]
    
  5. Using regular expressions to split strings in C#

    Employ regular expressions for more complex splitting patterns.

    using System.Text.RegularExpressions;
    
    string input = "apple,orange;banana";
    string[] fruits = Regex.Split(input, "[,;]");
    
    // Result: ["apple", "orange", "banana"]
    
  6. C# split string by whitespace

    Split a string into substrings based on whitespace.

    string sentence = "C# is great!";
    string[] words = sentence.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
    
    // Result: ["C#", "is", "great!"]
    
  7. C# split string and remove empty entries

    Remove empty entries from the result.

    string input = "apple,,banana";
    string[] fruits = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    
    // Result: ["apple", "banana"]
    
  8. Splitting a string into substrings of fixed length in C#

    Divide a string into substrings of a specified length.

    string input = "abcdefgh";
    string[] substrings = Enumerable.Range(0, input.Length / 3)
                                     .Select(i => input.Substring(i * 3, 3))
                                     .ToArray();
    
    // Result: ["abc", "def", "gh"]
    
  9. C# split string into an array of integers

    Convert the split substrings into an array of integers.

    string numbers = "1,2,3";
    int[] integers = numbers.Split(',').Select(int.Parse).ToArray();
    
    // Result: [1, 2, 3]
    
  10. C# split string and convert to lowercase/uppercase

    Convert the split substrings to lowercase or uppercase.

    string input = "Apple,Orange,Banana";
    string[] fruits = input.Split(',').Select(fruit => fruit.ToLower()).ToArray();
    
    // Result: ["apple", "orange", "banana"]