Answered by:
How to extract the Sepeated Segments by commas from a string?

Question
-
User-786564416 posted
I have a string "TagsList" enetered by user and it is separated by Commas, for example: "New York, Florida, Washington, Texas, California, Seattle,"
I want to make a loop, and inside it I want to set variable "TagValue" that extracts next segment of string between the commas.
Friday, October 5, 2018 10:46 PM
Answers
-
User409696431 posted
Split the string into a string array, and then fetch the values of the array. Trim the spaces, if you want to.
string taglist = "New York, Paris, Boston"; string[] words = taglist.Split(','); foreach (var word in words) { string city = word.Trim(); System.Console.WriteLine($"{city}"); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 5, 2018 10:58 PM -
User475983607 posted
I have a string "TagsList" enetered by user and it is separated by Commas, for example: "New York, Florida, Washington, Texas, California, Seattle,"
I want to make a loop, and inside it I want to set variable "TagValue" that extracts next segment of string between the commas.
You can tokenize the string into a collection using string.Split(','). How to determine what "next" means is up to you.
For example...
static void Main(string[] args) { string csv = @"New York, Florida, Washington, Texas, California, Seattle,"; string[] items = csv.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int i = 0; Console.WriteLine(items[i].Trim()); //Get the next item Console.WriteLine(items[i+1].Trim()); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 5, 2018 11:04 PM
All replies
-
User409696431 posted
Split the string into a string array, and then fetch the values of the array. Trim the spaces, if you want to.
string taglist = "New York, Paris, Boston"; string[] words = taglist.Split(','); foreach (var word in words) { string city = word.Trim(); System.Console.WriteLine($"{city}"); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 5, 2018 10:58 PM -
User475983607 posted
I have a string "TagsList" enetered by user and it is separated by Commas, for example: "New York, Florida, Washington, Texas, California, Seattle,"
I want to make a loop, and inside it I want to set variable "TagValue" that extracts next segment of string between the commas.
You can tokenize the string into a collection using string.Split(','). How to determine what "next" means is up to you.
For example...
static void Main(string[] args) { string csv = @"New York, Florida, Washington, Texas, California, Seattle,"; string[] items = csv.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int i = 0; Console.WriteLine(items[i].Trim()); //Get the next item Console.WriteLine(items[i+1].Trim()); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 5, 2018 11:04 PM -
User-786564416 posted
Thanks
Friday, October 5, 2018 11:09 PM