String split trim
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Program { static void Main(string[] args) { string plants = "Coneflowe, Deadnettle, Dewberry, Dindle, Drumstick"; Console.WriteLine(plants); //this line split string by comma and trim values and create string array. string[] trimmedSplittedArray = plants.Split(',').Select(x => x.Trim()).ToArray(); //this line only split string by comma and create string array string[] splittedArray = plants.Split(','); Console.WriteLine("\nstring splitted array trimmed elements......"); Array.ForEach(trimmedSplittedArray, Console.WriteLine); Console.WriteLine("\nstring splitted array elements......"); Array.ForEach(splittedArray, Console.WriteLine); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Coneflowe, Deadnettle, Dewberry, Dindle, Drumstick string splitted array trimmed elements...... Coneflowe Deadnettle Dewberry Dindle Drumstick string splitted array elements...... Coneflowe Deadnettle Dewberry Dindle Drumstick |