format-string

How do I format a string with string interpolation in Scala as a fixed width string?

匆匆过客 提交于 2020-01-11 01:26:51
问题 I'm interfacing with a really old system and the file I need to generate needs a field that is a formed from a string but needs to be exactly 15 in width. I want something like this: val companyName = "FooBar, Inc" // 11 chars f"$companyName%s" To return: " FooBar, Inc" Is there a slick way to do what I'm trying to do with the String interpolation? 回答1: Use String.format with a format string. Surely something there will do what you want :-) This code would do what you want: scala> val

Parsing the DateTime format to get Format String

梦想的初衷 提交于 2020-01-10 19:46:11
问题 I would like to be able to get the format string from a DateTime string. e.g. "2012-12-08 15:00:00" => "yyyy-MM-dd HH:mm:ss" "2013/30/01 16:00" => "yyyy/dd/MM HH:mm" Is this possible? 回答1: It would be very hard to do this in a completely general way, but one option would be to extract the relevant DateTimeFormatInfo that you're interested in (using CultureInfo.DateTimeFormat), extract the culture-specific patterns from that ( LongDatePattern , LongTimePattern etc), combine the patterns

Is %dn a format string?

℡╲_俬逩灬. 提交于 2020-01-07 06:43:51
问题 I recently came across this line in a code - fprintf(logfile," |-IP Version : %dn",(unsigned int)iph->version); Is "%dn" here a format string ? If so , what does it signify ? 回答1: It sounds a bit like someone wanted to write %d\n to terminate the line with a linefeed, but the backslash got lost somewhere. The format code, in any case, ends with the "d". 回答2: No, %d is a format string, signifying decimal value. 'n' will be appended. Unless it's '\n', which it probably is supposed to be, which

What does an 'F' in the Format specifier of printf mean?

爷,独闯天下 提交于 2020-01-06 02:59:33
问题 What does %.8Ff format specifier in printf do? What does F mean? 回答1: According to the manual: The F conversion specifier produces "INF", "INFINITY", or "NAN" instead of "inf", "infinity", or "nan", respectively. In your format string %.8Ff , the f is treated as a literal character and printed as an f . 回答2: One possible use of putting a literal f after the format specifier could be to print a string that's later to be parsed by a C or C++ compiler as a float constant instead of double

onFormat string For NSPredicate

别说谁变了你拦得住时间么 提交于 2019-12-31 05:24:08
问题 I want to sort data with "id" key, how can I understand what is format string for NSPredicate format ? I have an 100 number post. My code : let objectIDs = posts.map{$0.id} let predicate = NSPredicate(format: "self IN %@", objectIDs) let sortByUserId = NSSortDescriptor(key: "id", ascending: true) I have an error with this description: can not parse "self IN %@" format string. Code for Parsing JSON Data: func postsFromJSONData(data : NSData, inContext context: NSManagedObjectContext) ->

Does printf() allocate memory in C?

对着背影说爱祢 提交于 2019-12-30 11:08:40
问题 This simple method just creates an array of dynamic size n and initializes it with values 0 ... n-1. It contains a mistake, malloc() allocates just n instead of sizeof(int) * n bytes: int *make_array(size_t n) { int *result = malloc(n); for (int i = 0; i < n; ++i) { //printf("%d", i); result[i] = i; } return result; } int main() { int *result = make_array(8); for (int i = 0; i < 8; ++i) { printf("%d ", result[i]); } free(result); } When you check the output you will see that it will print

What determines (how to configure) what format string is used by php PDO driver for values of date and timestamp fields?

倖福魔咒の 提交于 2019-12-24 23:04:07
问题 I have Firebird 3.0 database which have date and timestamp fields and I am using interbase extension (yes - still interbase) and Yii2 framwork in PHP 7.x. And I have Yii code: Yii::$app->db->createCommand('select order_date from orders where order_id=5'); which returns field with value: 2019-10-15 00:00:00 AFAIU then Yii2 uses PDO and that is why my question reduces to the question about PDO. PHP has DateTime extension but AFAIU PDO does not use DateTime class (and also does not use Unix

UnicodeDecodeError using Django and format-strings

醉酒当歌 提交于 2019-12-22 03:46:41
问题 I wrote a small example of the issue for everybody to see what's going on using Python 2.7 and Django 1.10.8 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals, print_function import time from django import setup setup() from django.contrib.auth.models import Group group = Group(name='schön') print(type(repr(group))) print(type(str(group))) print(type(unicode(group))) print(group) print(repr(group)) print(str(group)) print(unicode(group)) time.sleep(1.0

How can a Format-String vulnerability be exploited?

孤者浪人 提交于 2019-12-17 02:58:26
问题 I was reading about vulnerabilities in code and came across this Format-String Vulnerability . Wikipedia says: Format string bugs most commonly appear when a programmer wishes to print a string containing user supplied data. The programmer may mistakenly write printf(buffer) instead of printf("%s", buffer). The first version interprets buffer as a format string, and parses any formatting instructions it may contain. The second version simply prints a string to the screen, as the programmer

Easy way to check FormatString is valid?

谁说胖子不能爱 提交于 2019-12-12 21:14:35
问题 Is there an easy way to check if a format string is valid? For example the following is code that we use to test a number format string; public static bool IsValidFormatStringNumber(string FormatString) { try { const decimal number = 0.056m; var formattedNumber = number.ToString(FormatString); return formattedNumber.Length > 0; } catch { return false; } } We're trying to catch an exception or determine if the resulting string has no length. This test fails however as a format string of