Get the first integers in a string with JavaScript

If the number is at the start of the string: (“123 hello everybody 4″).replace(/(^\d+)(.+$)/i,’$1′); //=> ‘123’ If it’s somewhere in the string: (” hello 123 everybody 4″).replace( /(^.+)(\w\d+\w)(.+$)/i,’$2′); //=> ‘123’ And for a number between characters: (“hello123everybody 4”).replace( /(^.+\D)(\d+)(\D.+$)/i,’$2′); //=> ‘123’ [addendum] A regular expression to match all numbers in a string: “4567 stuff is … Read more

Using !s vs. :s to format a string in Python

!s, and its brethren !a and !r apply str(), ascii() and repr() respectively before interpolation and formatting. These are called conversion flags, and are part of the Format String Syntax spec, not the per-field formatting spec applied to values when interpolating: The conversion field causes a type coercion before formatting. Normally, the job of formatting … Read more

Join list of string to comma separated and enclosed in single quotes

This should work: List<string> test = new List<string>(); test.Add(“test’s”); test.Add(“test”); test.Add(“test’s more”); string s = string.Join(“‘,'”, test.Select(i => i.Replace(“‘”, “””))); And if you’re really looking to enclose the whole thing in single quotes: string s = string.Format(“‘{0}'”, string.Join(“‘,'”, test.Select(i => i.Replace(“‘”, “””))));

Does Python do variable interpolation similar to “string #{var}” in Ruby? [duplicate]

Python 3.6+ does have variable interpolation – prepend an f to your string: f”foo is {bar}” For versions of Python below this (Python 2 – 3.5) you can use str.format to pass in variables: # Rather than this: print(“foo is #{bar}”) # You would do this: print(“foo is {}”.format(bar)) # Or this: print(“foo is {bar}”.format(bar=bar)) … Read more

How can I append a formatted string to an existing String?

I see now that String implements Write, so we can use write!: use std::fmt::Write; pub fn main() { let mut a = “hello “.to_string(); write!(a, “{}”, 5).unwrap(); println!(“{}”, a); assert_eq!(“hello 5”, a); } (Playground) It is impossible for this write! call to return an Err, at least as of Rust 1.47, so the unwrap should … Read more

Using .format() to format a list with field width arguments

The error message ValueError: cannot switch from manual field specification to automatic field numbering pretty much says it all: You need to give explicit field indices everwhere, and print(‘{0[0]:{1}s} {0[1]:{2}d} {0[2]:{3}f}’.format(res, 10, 5, .2)) works fine. If you want to use .format(res, args), you can specify all indices in the format string like that: >>> … Read more

WPF StringFormat={0:C} showing as dollars

I’m not sure if this has been fixed in .NET 4, but WPF has never picked up the current culture when rendering things like currency or dates. It’s something I consider a massive oversight, but thankfully is easily corrected. In your App class: protected override void OnStartup(StartupEventArgs e) { FrameworkElement.LanguageProperty.OverrideMetadata( typeof(FrameworkElement), new FrameworkPropertyMetadata( XmlLanguage.GetLanguage( CultureInfo.CurrentCulture.IetfLanguageTag))); … Read more

Number formatting in template strings (Javascript – ES6)

No, ES6 does not introduce any new number formatting functions, you will have to live with the existing .toExponential(fractionDigits), .toFixed(fractionDigits), .toPrecision(precision), .toString([radix]) and toLocaleString(…) (which has been updated to optionally support the ECMA-402 Standard, though). Template strings have nothing to do with number formatting, they just desugar to a function call (if tagged) or string … Read more