by
0
7
475k
5
Top 1% !
Popular
Pearl of Wisdom
Tagged
Specified
Refined
OpenSource
Popularity: 1498th place
Created
Modified Jan 31, 2014

Published on:

.NETFramework4.0
Architecturex86_64
Extension.cs
InputName0content
InputName1separator
InputType0string
InputType1char
LanguageCSharp
LicenseMIT_X11
MethodNameParseIntoListOfLongs
OSWindows
OutputTypeList<long>

C#: Parse a comma separated string into a collection of numeric items

This method can be used with any separator you need, just setting the 'separator' input
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/25657/C---Parse-a-comma-separated-string-into-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static List<long> ParseIntoListOfLongs(string content, char separator = ',') { List<long> ids = new List<long>(); if (string.IsNullOrEmpty(content)) return ids; string[] strIds = content.Split(separator); foreach (string str in strIds) { long id = -1; if (long.TryParse(str, out id)) { if (id > 0) ids.Add(id); } } return ids; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus