Formatting Phone number in Swift

Masked number typing /// mask example: `+X (XXX) XXX-XXXX` func format(with mask: String, phone: String) -> String { let numbers = phone.replacingOccurrences(of: “[^0-9]”, with: “”, options: .regularExpression) var result = “” var index = numbers.startIndex // numbers iterator // iterate over the mask characters until the iterator of numbers ends for ch in mask where … Read more

advanced string formatting vs template strings

Templates are meant to be simpler than the the usual string formatting, at the cost of expressiveness. The rationale of PEP 292 compares templates to Python’s %-style string formatting: Python currently supports a string substitution syntax based on C’s printf() ‘%’ formatting character. While quite rich, %-formatting codes are also error prone, even for experienced … Read more

Clever way to append ‘s’ for plural form in .Net (syntactic sugar)

You may checkout the PluralizationService class which is part of the .NET 4.0 framework: string lives = “life”; if (player.Lives != 1) { lives = PluralizationService .CreateService(new CultureInfo(“en-US”)) .Pluralize(lives); } Console.WriteLine(“You have {0} {1} left”, player.Lives, lives); It is worth noting that only English is supported for the moment. Warning, this don’t work on the … Read more

How to format message with argument names instead of numbers?

You can use MapFormat for this. Find out the details here: http://www.java2s.com/Code/Java/I18N/AtextformatsimilartoMessageFormatbutusingstringratherthannumerickeys.htm String text = “The user {name} has email address {email}.”; Map map = new HashMap(); map.put(“name”, “Robert”); map.put(“email”, “rhume55@gmail.com”); System.out.println(“1st : ” + MapFormat.format(text, map)); OUTPUT: 1st : The user Robert has email address rhume55@gmail.com.

How can I use f-string with a variable, not with a string literal?

f”…” strings are great when interpolating expression results into a literal, but you don’t have a literal, you have a template string in a separate variable. You can use str.format() to apply values to that template: name=[“deep”,”mahesh”,”nirbhay”] user_input = “certi_{element}” # this string i ask from user for value in name: print(user_input.format(element=value)) String formatting placeholders … Read more

How to format localised strings in Swift?

You can use %@ in Swift’s String(format:…), it can be substituted by a Swift String or any instance of a NSObject subclass. For example, if the Localizable.strings file contains the definition “From %@, %@” = “从 %@, %@ 得出”; then let x = 1.2 let y = 2.4 let text = String(format: NSLocalizedString(“From %@, %@”, … Read more

JavaScript Chart.js – Custom data formatting to display on tooltip

For chart.js 2.0+, this has changed (no more tooltipTemplate/multiTooltipTemplate). For those that just want to access the current, unformatted value and start tweaking it, the default tooltip is the same as: options: { tooltips: { callbacks: { label: function(tooltipItem, data) { return tooltipItem.yLabel; } } } } I.e., you can return modifications to tooltipItem.yLabel, which … Read more