String concatenation vs. string substitution in Python

Concatenation is (significantly) faster according to my machine. But stylistically, I’m willing to pay the price of substitution if performance is not critical. Well, and if I need formatting, there’s no need to even ask the question… there’s no option but to use interpolation/templating. >>> import timeit >>> def so_q_sub(n): … return “%s%s/%d” % (DOMAIN, … 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

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

How to concatenate Strings in EL expression?

If you’re already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this: <h:commandButton … action=”#{someController.doSomething(id += ‘SomeTableId’)}” /> If you’re however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use … Read more

How to split string preserving whole words?

Try this: static void Main(string[] args) { int partLength = 35; string sentence = “Silver badges are awarded for longer term goals. Silver badges are uncommon.”; string[] words = sentence.Split(‘ ‘); var parts = new Dictionary<int, string>(); string part = string.Empty; int partCounter = 0; foreach (var word in words) { if (part.Length + word.Length … Read more