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 separate joined strings in C#
In C#, you can join string arrays into a single string using string.Join() and then separate the joined string back into individual elements using the Split() method. This is useful when you need to manipulate strings by combining them temporarily and then extracting the original components.
Syntax
Following is the syntax for joining strings −
string joinedString = string.Join(separator, stringArray);
Following is the syntax for separating joined strings −
string[] separatedArray = joinedString.Split(separator);
Parameters
-
separator − The character or string used to separate elements during joining and splitting.
-
stringArray − The array of strings to be joined.
-
joinedString − The combined string that will be split back into individual elements.
Example
using System;
public class Demo {
public static void Main() {
string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };
// join words
string res = string.Join(" ", str);
Console.WriteLine("Joined Strings...
" + res);
string[] convert = res.Split(' ');
Console.WriteLine("\nSeparate Joined Strings...");
foreach (string val in convert) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Joined Strings... Java AngularJS Python jQuery HTML5 Separate Joined Strings... Java AngularJS Python jQuery HTML5
Using Different Separators
Example
using System;
public class Program {
public static void Main() {
string[] languages = { "C#", "Java", "Python", "JavaScript" };
// Join with comma separator
string commaSeparated = string.Join(",", languages);
Console.WriteLine("Comma separated: " + commaSeparated);
// Split back using comma
string[] splitByComma = commaSeparated.Split(',');
Console.WriteLine("Split by comma:");
foreach (string lang in splitByComma) {
Console.WriteLine("- " + lang);
}
// Join with pipe separator
string pipeSeparated = string.Join("|", languages);
Console.WriteLine("\nPipe separated: " + pipeSeparated);
// Split back using pipe
string[] splitByPipe = pipeSeparated.Split('|');
Console.WriteLine("Split by pipe:");
foreach (string lang in splitByPipe) {
Console.WriteLine("- " + lang);
}
}
}
The output of the above code is −
Comma separated: C#,Java,Python,JavaScript Split by comma: - C# - Java - Python - JavaScript Pipe separated: C#|Java|Python|JavaScript Split by pipe: - C# - Java - Python - JavaScript
Common Use Cases
-
Data Processing − Converting between array format and delimited strings for file I/O.
-
Configuration Files − Storing multiple values as comma-separated strings.
-
URL Parameters − Building and parsing query strings with multiple values.
-
CSV Processing − Handling comma-separated values in data files.
Conclusion
The combination of string.Join() and Split() methods provides a powerful way to convert between string arrays and delimited strings. This technique is essential for data processing tasks where you need to temporarily combine strings and then separate them back into individual components.
