Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to split a string on spaces
In C#, the Split() method is used to divide a string into an array of substrings based on a specified delimiter. When splitting a string on spaces, we use the space character ' ' as the delimiter.
Syntax
Following is the syntax for splitting a string on spaces −
string[] result = str.Split(' ');
You can also use the overloaded version with options −
string[] result = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
Parameters
-
separator − The character used to split the string (space character
' 'in this case). -
options − Optional parameter to specify split behavior such as removing empty entries.
Return Value
The Split() method returns a string[] array containing the substrings separated by the specified delimiter.
Using Split() with Single Space
Example
using System;
class Program {
static void Main() {
string str = "Science and Mathematics";
Console.WriteLine("Original String: " + str);
string[] words = str.Split(' ');
Console.WriteLine("\nSplit Result:");
for (int i = 0; i < words.Length; i++) {
Console.WriteLine("Index " + i + ": " + words[i]);
}
}
}
The output of the above code is −
Original String: Science and Mathematics Split Result: Index 0: Science Index 1: and Index 2: Mathematics
Handling Multiple Spaces
When a string contains multiple consecutive spaces, the Split() method creates empty entries. You can use StringSplitOptions.RemoveEmptyEntries to avoid this −
Example
using System;
class Program {
static void Main() {
string str = "Hello World C#";
Console.WriteLine("Original String: '" + str + "'");
// Without removing empty entries
string[] wordsWithEmpty = str.Split(' ');
Console.WriteLine("\nWith empty entries:");
foreach (string word in wordsWithEmpty) {
Console.WriteLine("'" + word + "'");
}
// Removing empty entries
string[] wordsClean = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("\nWithout empty entries:");
foreach (string word in wordsClean) {
Console.WriteLine("'" + word + "'");
}
}
}
The output of the above code is −
Original String: 'Hello World C#' With empty entries: 'Hello' '' '' '' 'World' '' '' 'C#' Without empty entries: 'Hello' 'World' 'C#'
Using Split() with Multiple Delimiters
You can split on multiple characters including spaces, tabs, and other whitespace characters −
Example
using System;
class Program {
static void Main() {
string str = "Apple,Banana Orange;Grape\tMango";
Console.WriteLine("Original String: " + str);
char[] delimiters = {' ', ',', ';', '\t'};
string[] fruits = str.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("\nExtracted words:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original String: Apple,Banana Orange;Grape Mango Extracted words: Apple Banana Orange Grape Mango
Conclusion
The Split() method in C# is an effective way to break strings into arrays based on delimiters like spaces. Use StringSplitOptions.RemoveEmptyEntries when dealing with multiple consecutive spaces to avoid empty elements in the result array.
