Turning a Comma Separated string into individual rows

You can use the wonderful recursive functions from SQL Server: Sample table: CREATE TABLE Testdata ( SomeID INT, OtherID INT, String VARCHAR(MAX) ); INSERT Testdata SELECT 1, 9, ‘18,20,22’; INSERT Testdata SELECT 2, 8, ‘17,19’; INSERT Testdata SELECT 3, 7, ‘13,19,20’; INSERT Testdata SELECT 4, 6, ”; INSERT Testdata SELECT 9, 11, ‘1,2,3,4’; The query … Read more

Split a List into smaller lists of N size [duplicate]

I would suggest to use this extension method to chunk the source list to the sub-lists by specified chunk size: /// <summary> /// Helper methods for the lists. /// </summary> public static class ListExtensions { public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize) { return source .Select((x, i) => new { Index = i, Value … Read more

How to split one string into multiple strings separated by at least one space in bash shell?

I like the conversion to an array, to be able to access individual elements: sentence=”this is a story” stringarray=($sentence) now you can access individual elements directly (it starts with 0): echo ${stringarray[0]} or convert back to string in order to loop: for i in “${stringarray[@]}” do : # do whatever on $i done Of course … Read more

Splitting on last delimiter in Python string?

Use .rsplit() or .rpartition() instead: s.rsplit(‘,’, 1) s.rpartition(‘,’) str.rsplit() lets you specify how many times to split, while str.rpartition() only splits once but always returns a fixed number of elements (prefix, delimiter & postfix) and is faster for the single split case. Demo: >>> s = “a,b,c,d” >>> s.rsplit(‘,’, 1) [‘a,b,c’, ‘d’] >>> s.rsplit(‘,’, 2) … Read more

Java String split removed empty values

split(delimiter) by default removes trailing empty strings from result array. To turn this mechanism off we need to use overloaded version of split(delimiter, limit) with limit set to negative value like String[] split = data.split(“\\|”, -1); Little more details: split(regex) internally returns result of split(regex, 0) and in documentation of this method you can find … Read more