string-formatting

VBScript: What is the simplest way to format a string?

北战南征 提交于 2019-12-17 06:53:02
问题 I have the following format: Value1 is {0} and Value2 is {1}. I need to replace the numbers in the brackets with strings. This is easily done in most languages using string.Format or something along those lines. How can I do this using only vbscript? I've tried: Replace (strFormat, "{0}", value1) Replace (strFormat, "{1}", value2) It does not work. Any solutions? 回答1: Replace (strFormat, "{0}", value1) Based on your code snip, I'm guessing you believe Replace mutates strFormat directly. It

How can I extract keywords from a Python format string?

回眸只為那壹抹淺笑 提交于 2019-12-17 06:41:10
问题 I want to provide automatic string formatting in an API such that: my_api("path/to/{self.category}/{self.name}", ...) can be replaced with the values of attributes called out in the formatting string. How do I extract the keyword arguments from a Python format string: "non-keyword {keyword1} {{escaped brackets}} {} {keyword2}" => 'keyword1', 'keyword2' 回答1: You can use the string.Formatter() class to parse out the fields in a string, with the Formatter.parse() method: from string import

Python TypeError: non-empty format string passed to object.__format__

南笙酒味 提交于 2019-12-17 04:01:12
问题 I hit this TypeError exception recently, which I found very difficult to debug. I eventually reduced it to this small test case: >>> "{:20}".format(b"hi") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: non-empty format string passed to object.__format__ This is very non-obvious, to me anyway. The workaround for my code was to decode the byte string into unicode: >>> "{:20}".format(b"hi".decode("ascii")) 'hi ' What is the meaning of this exception? Is there a

Python TypeError: non-empty format string passed to object.__format__

萝らか妹 提交于 2019-12-17 04:01:05
问题 I hit this TypeError exception recently, which I found very difficult to debug. I eventually reduced it to this small test case: >>> "{:20}".format(b"hi") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: non-empty format string passed to object.__format__ This is very non-obvious, to me anyway. The workaround for my code was to decode the byte string into unicode: >>> "{:20}".format(b"hi".decode("ascii")) 'hi ' What is the meaning of this exception? Is there a

Convert hex to binary

冷暖自知 提交于 2019-12-17 03:50:00
问题 I have ABC123EFFF. I want to have 001010101111000001001000111110111111111111 (i.e. binary repr. with, say, 42 digits and leading zeroes). How? 回答1: For solving the left-side trailing zero problem: my_hexdata = "1a" scale = 16 ## equals to hexadecimal num_of_bits = 8 bin(int(my_hexdata, scale))[2:].zfill(num_of_bits) It will give 00011010 instead of the trimmed version. 回答2: import binascii binary_string = binascii.unhexlify(hex_string) Read binascii.unhexlify Return the binary data

Format a Go string without printing?

我与影子孤独终老i 提交于 2019-12-17 02:07:34
问题 Is there a simple way to format a string in Go without printing the string? I can do: bar := "bar" fmt.Printf("foo: %s", bar) But I want the formatted string returned rather than printed so I can manipulate it further. I could also do something like: s := "foo: " + bar But this becomes difficult to read when the format string is complex, and cumbersome when one or many of the parts aren't strings and have to be converted first, like i := 25 s := "foo: " + strconv.Itoa(i) Is there a simpler

C#: php sprintf equivalent

南楼画角 提交于 2019-12-14 04:04:59
问题 I'm looking for a C# equivalent for the php sprintf function. I have the folowing string: "There are %s hits, %s are an exact match." I want %s to be replaced with the numbers that the query returns. In php I would di the folowing: $res = sprintf("There are %s hits, %s are an exact match", $number1, $number2); How do I do this in C#? I thought about string.replace() but that would only work with 1 piece that should be replaced. In this case there are multiple. 回答1: You are looking for String

Looping through Array in PHP returned from MySQL Query

☆樱花仙子☆ 提交于 2019-12-14 02:35:40
问题 I have a part of an application that loops through a return of a MySQL query (as we all know) in the form of an Array. However, I need several different format settings placed on some of the items returned, for example, one column needs Japanese currency, the other has American currency and one of the returned items is a link to an image. I would use the names of the column, however this same function that I am using to accomplish this will be used for many different tables. This is what I

How to read a sequence of space separated integers until newline character is encountered?

人走茶凉 提交于 2019-12-13 23:32:04
问题 I have been trying to write a program that would read a sequence of space separated integers until the newline character is encountered.My approach was to read the input as a string and using atoi() to convert the string to integer. This is my approach: #include<stdio.h> #include<stdlib.h> #include<ctype.h> int main() { int a[100],i=0,k=0; char s[100]; //Read the first character scanf("%c",&s[i]); //Reads characters until new line character is encountered while(s[i]!='\n'){ i+=1; scanf("%c",

Custom string formatting: ToString(“00”)

痞子三分冷 提交于 2019-12-13 22:57:00
问题 Given the following code: string istanbul = "523"; Convert.ToInt32(istanbul.ToString("00")); what does it return? 回答1: The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero after the decimal point