Longest common prefix of two strings in bash
In sed, assuming the strings don’t contain any newline characters: string1=”test toast” string2=”test test” printf “%s\n%s\n” “$string1” “$string2” | sed -e ‘N;s/^\(.*\).*\n\1.*$/\1/’
In sed, assuming the strings don’t contain any newline characters: string1=”test toast” string2=”test test” printf “%s\n%s\n” “$string1” “$string2” | sed -e ‘N;s/^\(.*\).*\n\1.*$/\1/’
Although not an “answer”, I can give you my results on a slightly newer version running on AIX. Sorry that I’m unable to replicate your problem. [lholtscl@ibm3 ~]$ python Python 2.7.13 (default, Sep 7 2017, 21:08:50) [C] on aix7 Type “help”, “copyright”, “credits” or “license” for more information. >>> print “%.10f” % 123456789012 123456789012.0000000000 >>> … Read more
The recommendation in PEP 3101 is to subclass Formatter: import string class PartialFormatter(string.Formatter): def __init__(self, missing=’~~’, bad_fmt=”!!”): self.missing, self.bad_fmt=missing, bad_fmt def get_field(self, field_name, args, kwargs): # Handle a key not found try: val=super(PartialFormatter, self).get_field(field_name, args, kwargs) # Python 3, ‘super().get_field(field_name, args, kwargs)’ works except (KeyError, AttributeError): val=None,field_name return val def format_field(self, value, spec): # handle … Read more
See the Qt docs about QString::arg(): QString str; str = “%1 %2”; str.arg(“%1f”, “Hello”); // returns “%1f Hello”
You’ve done it. this is argument 1, so by saying format(printf, 2, 3) you’re telling the compiler that you’re NOT printing this, you’re printing argument 2 (fmt) with additional arguments past that.
It looks like the Right Way to do this in F# 2.0 is by using the StructuredFormatDisplay attribute, for example: [<StructuredFormatDisplay(“hello {a}”)>] type myType = {a: int} In this example, instead of the default {a = 42;}, you would get hello 42. This works the same way for object, record, and union types. And although … Read more
To preserve the precision for a BigDecimal you need to pass the value in as a String BigDecimal d = new BigDecimal(“12334535345456700.12345634534534578901”); System.out.println(d.toPlainString());
You can define a custom formatter that returns “NULL” if the value is null and otherwise the default formatted string, e.g.: foreach (var value in new[] { 123456.78m, -123456.78m, 0m, (decimal?)null }) { string result = string.Format( new NullFormat(), “${0:#,000.00;(#,000.00);ZERO}”, value); Console.WriteLine(result); } Output: $123.456,78 $(123.456,78) $ZERO $NULL Custom Formatter: public class NullFormat : IFormatProvider, … Read more
You can use the .attribute_name notation inside the format fields themselves: print ‘My object has strings a={0.a}, b={0.b}, c={0.c}’.format(obj) Below is a demonstration: >>> class Test(object): … def __init__(self, a, b, c): … self.a = a … self.b = b … self.c = c … >>> obj = Test(1, 2, 3) >>> ‘My object has … Read more
slf4j’s api provides “parameterized logging”, which allows you to do exactly that, although with a slightly different syntax. The example there is: logger.debug(“Value {} was inserted between {} and {}.”, newVal, below, above); For an implementation, you can use Logback which implements slf4j natively, or the slf4j bindings to connect with log4j or other loggers. … Read more