How to split string into Array of strings using C# ?
Freshers face situations where they need to split lengthy String into separate and small strings. In order to achieve this goal we need to think on which basis we are going to split that lengthy string. Whether that character is a special symbol or an alphabet or just a space. The good thing is .Net Framework provides a built-in function to make this happen easily. We will see in following simple program how to do it.
Code Snippet:-
public class SplitingString
{
public static void Main()
{
// Display message to enter lengthy string
Console.WriteLine("Enter the complete string");
// The input is a variable to store entered string
string input = Console.ReadLine();
// Splitting string on the basis of space and storing into an output string array
string[] output = input.Split(' ');
// Looping through the array to display values one by one
foreach (var word in output)
{
Console.WriteLine(word);
}
Console.ReadLine();
}
}
So, in the above example we are spiting string on the basis of Space(" "). As we mention we can choose anything on the basis of requirement. The Output is coming in the array, so you could use it inflexible way. If you provide "Today is Holiday" then the output will be like below,
"Today" => 0 position of Output Array,
"is"=> 1 position of Output Array and
"Holiday"=> 2 position of Output Array.
Code Snippet:-
public class SplitingString
{
public static void Main()
{
// Display message to enter lengthy string
Console.WriteLine("Enter the complete string");
// The input is a variable to store entered string
string input = Console.ReadLine();
// Splitting string on the basis of space and storing into an output string array
string[] output = input.Split(' ');
// Looping through the array to display values one by one
foreach (var word in output)
{
Console.WriteLine(word);
}
Console.ReadLine();
}
}
So, in the above example we are spiting string on the basis of Space(" "). As we mention we can choose anything on the basis of requirement. The Output is coming in the array, so you could use it inflexible way. If you provide "Today is Holiday" then the output will be like below,
"Today" => 0 position of Output Array,
"is"=> 1 position of Output Array and
"Holiday"=> 2 position of Output Array.
Comments
Post a Comment