Using LINQ to concatenate strings

This answer shows usage of LINQ (Aggregate) as requested in the question and is not intended for everyday use. Because this does not use a StringBuilder it will have horrible performance for very long sequences. For regular code use String.Join as shown in the other answer Use aggregate queries like this: string[] words = { … Read more

Format specifier for integer variables in format() for EXECUTE?

This would be shorter, faster and safer: CREATE OR REPLACE FUNCTION get_parent_ltree(parent_id bigint, tbl_name regclass , OUT parent_ltree ltree) LANGUAGE plpgsql AS $func$ BEGIN EXECUTE format(‘SELECT l_tree FROM %s WHERE id = $1′, tbl_name) INTO parent_ltree USING parent_id; END $func$; Why? Most importantly, use the USING clause of EXECUTE for parameter values. Don’t convert them … Read more

Group subarrays by one column, make comma-separated values from other column within groups

There should be more elegant solutions, but simplest one I can think of would be this. // The data you have pasted in the question $data = []; $groups = []; // Go through the entire array $data foreach($data as $item){ // If the key doesn’t exist in the new array yet, add it if(!array_key_exists($item[1], … Read more

Does concatenating strings in Java always lead to new strings being created in memory?

I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit. No it won’t. Since these are string literals, they will be evaluated at compile time and only one string will be created. This is defined in the Java Language Specification … Read more

How to concatenate strings with padding in sqlite

The || operator is “concatenate” – it joins together the two strings of its operands. From For padding, the seemingly-cheater way I’ve used is to start with your target string, say ‘0000’, concatenate ‘0000423’, then substr(result, -4, 4) for ‘0423’. Update: Looks like there is no native implementation of “lpad” or “rpad” in SQLite, … Read more

String concatenation in Ruby

You can do that in several ways: As you shown with << but that is not the usual way With string interpolation source = “#{ROOT_DIR}/#{project}/App.config” with + source = “#{ROOT_DIR}/” + project + “/App.config” The second method seems to be more efficient in term of memory/speed from what I’ve seen (not measured though). All three … Read more