string-interpolation

Specify decimal places using variables inside string interpolation

蓝咒 提交于 2019-12-07 07:05:22
问题 I have a string format which includes two integer variables, each of which needs to be formatted to a variable length: int x = 1234; int y = 42; // Simplified, real values come from method outputs, so must use the variables: int xFormatDigitCount = 7; int yFormatDigitCount = 3; var xStringFormat = new string('0', xFormatDigitCount); // "0000000" var yStringFormat = new string('0' ,yFormatDigitCount); // "000" For now I only managed to get the desired format using the integer variables'

Transform string to f-string

一世执手 提交于 2019-12-06 22:41:50
问题 How do I transform a classic string to an f-string ? : variable = 42 user_input = "The answer is {variable}" print(user_input) The answer is {variable} f_user_input = # Here the operation to go from a string to an f-string print(f_user_input) The answer is 42 回答1: An f-string is syntax , not an object type. You can't convert an arbitrary string to that syntax, the syntax creates a string object, not the other way around. I'm assuming you want to use user_input as a template, so just use the

Which of one from string interpolation and string.format is better in performance?

南楼画角 提交于 2019-12-05 18:51:51
问题 Consider this code: var url = "www.example.com"; String.Format: var targetUrl = string.Format("URL: {0}", url); String Interpolation: var targetUrl=$"URL: {url}"; Which of one from string interpolation and string.Format is better in performance? Also what are the best fit scenarios for use of them? 回答1: Which of one from string interpolation and string.format is better in performance? Neither of them is better since they are equal on run-time. String interpolation is rewritten by the compiler

Scala String interpolation with Format, how to change locale?

我的未来我决定 提交于 2019-12-05 14:30:39
问题 When doing format string interpolation in Sweden I get a comma instead of a dot when creating strings with decimal numbers: scala> val a = 5.010 a: Double = 5.01 scala> val a = 5.0101 a: Double = 5.0101 scala> f"$a%.2f" res0: String = 5,01 My question is, how do I set the format so that I get the result 5.01 ? I would like to be able to set the locale only for that String, i.e. so that I don't change the locale for the whole environment. Cheers, Johan 回答1: Using the same Java library number

Dynamic string interpolation

馋奶兔 提交于 2019-12-05 10:44:35
问题 Can anyone help me with this? Required Output: " Todo job for admin " class Program { static void Main(string[] args) { Console.WriteLine(ReplaceMacro("{job.Name} job for admin", new Job { Id = 1, Name = "Todo", Description="Nothing" })); Console.ReadLine(); } static string ReplaceMacro(string value, Job job) { return value; //Output should be "Todo job for admin" } } class Job { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } 回答1: Two

Django translations and gettext: The deprecation of the % (string interpolation) operator

烂漫一生 提交于 2019-12-05 10:02:57
Although Django Django does not yet support Python 3, it eventually will, so I want to keep my code the more "future-proof" possible. Since Python 2.7 the string interpolation operator ( % ) is being deprecated. And I realized that every string that needs to be translated is using the % interpolation syntax. And in the Django docs there is no mention of the new str.format method (the "new" official way of string formatting)... Maybe there is a limitation of the gettext library, but I don't think so, since the string appears identical in the .PO files. The question is if I can use the new

Specify decimal places using variables inside string interpolation

血红的双手。 提交于 2019-12-05 08:54:45
I have a string format which includes two integer variables, each of which needs to be formatted to a variable length: int x = 1234; int y = 42; // Simplified, real values come from method outputs, so must use the variables: int xFormatDigitCount = 7; int yFormatDigitCount = 3; var xStringFormat = new string('0', xFormatDigitCount); // "0000000" var yStringFormat = new string('0' ,yFormatDigitCount); // "000" For now I only managed to get the desired format using the integer variables' .ToString() methods: var xString = x.ToString(xStringFormat); var yString = y.ToString(yStringFormat); return

ruby here documents

a 夏天 提交于 2019-12-05 05:35:33
问题 I am trying to write a method in Ruby that uses a here-document of HTML code with input variables and fills them in accordingly. My method is: calcForm(left, op, right, result) The html tags I am using are <input type="text" name="left" value="?????"> <select name="op"> <option value="add" ?????>+</option> <option value="mul" ?????>*</option> </select> <input type="text" name="right" value="?????"> = ????? Everywhere there are question marks my method has to fill in with variables left, op,

String interpolation in Swift

旧街凉风 提交于 2019-12-05 04:06:01
A function in swift takes any numeric type in Swift (Int, Double, Float, UInt, etc). the function converts the number to a string the function signature is as follows : func swiftNumbers <T : NumericType> (number : T) -> String { //body } NumericType is a custom protocol that has been added to numeric types in Swift. inside the body of the function, the number should be converted to a string: I use the following var stringFromNumber = "\(number)" which is not so elegant, PLUS : if the absolute value of the number is strictly inferior to 0.0001 it gives this: "\(0.000099)" //"9.9e-05" or if the

Transform string to f-string

﹥>﹥吖頭↗ 提交于 2019-12-05 02:28:58
How do I transform a classic string to an f-string ? : variable = 42 user_input = "The answer is {variable}" print(user_input) The answer is {variable} f_user_input = # Here the operation to go from a string to an f-string print(f_user_input) The answer is 42 An f-string is syntax , not an object type. You can't convert an arbitrary string to that syntax, the syntax creates a string object, not the other way around. I'm assuming you want to use user_input as a template, so just use the str.format() method on the user_input object: variable = 42 user_input = "The answer is {variable}" formatted