string-formatting

Should I use Java's String.format() if performance is important?

喜欢而已 提交于 2019-11-26 02:26:46
问题 We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer (many appends, thread safe) and StringBuilder (many appends, non-thread-safe). What\'s the advice on using String.format() ? Is it efficient, or are we forced to stick with concatenation for one-liners where performance is important? e.g. ugly old style, String s = \"What do you get if you multiply \" + varSix + \" by \" + varNine + \"?\"; vs. tidy new style (String

Python: Format output string, right alignment

南笙酒味 提交于 2019-11-26 01:58:09
问题 I am processing a text file containing coordinates x, y, z 1 128 1298039 123388 0 2 .... every line is delimited into 3 items using words = line.split() After processing data I need to write coordinates back in another txt file so as items in each column are aligned right (as well as the input file). Every line is composed of the coordinates line_new = words[0] + \' \' + words[1] + \' \' words[2]. Is there any manipulator like std::setw() etc. in C++ allowing to set the width and alignment?

Is it possible dynamically to add String to String.xml in Android?

风格不统一 提交于 2019-11-26 01:55:48
问题 Is it possible to have placeholders in string values in string.xml that can be assigned values at run time? Example: some string PLACEHOLDER1 some more string 回答1: Formatting and Styling Yes, see the following from String Resources: Formatting and Styling If you need to format your strings using String.format(String, Object...) , then you can do so by putting your format arguments in the string resource. For example, with the following resource: <string name="welcome_messages">Hello, %1$s!

Named string formatting in C#

倖福魔咒の 提交于 2019-11-26 01:46:27
问题 Is there any way to format a string by name rather than position in C#? In python, I can do something like this example (shamelessly stolen from here): >>> print \'%(language)s has %(#)03d quote types.\' % \\ {\'language\': \"Python\", \"#\": 2} Python has 002 quote types. Is there any way to do this in C#? Say for instance: String.Format(\"{some_variable}: {some_other_variable}\", ...); Being able to do this using a variable name would be nice, but a dictionary is acceptable too. 回答1: There

How to get 0-padded binary representation of an integer in java?

吃可爱长大的小学妹 提交于 2019-11-26 01:24:34
问题 for example, for 1, 2, 128, 256 the output can be (16 digits): 0000000000000001 0000000000000010 0000000010000000 0000000100000000 I tried String.format(\"%16s\", Integer.toBinaryString(1)); it puts spaces for left-padding: ` 1\' How to put 0 s for padding. I couldn\'t find it in Formatter. Is there another way to do it? P.S. this post describes how to format integers with left 0-padding, but it is not for the binary representation. 回答1: I think this is a suboptimal solution, but you could do

How to format strings in Java

梦想的初衷 提交于 2019-11-26 00:57:26
Primitive question, but how do I format strings like this: "Step {1} of {2}" by substituting variables using Java? In C# it's easy. In addition to String.format, also take a look java.text.MessageFormat . The format less terse and a bit closer to the C# example you've provided and you can use it for parsing as well. For example: int someNumber = 42; String someString = "foobar"; Object[] args = {new Long(someNumber), someString}; MessageFormat fmt = new MessageFormat("String is \"{1}\", number is {0}."); System.out.println(fmt.format(args)); A nicer example takes advantage of the varargs and

Display a decimal in scientific notation

夙愿已清 提交于 2019-11-26 00:47:36
问题 How can I display this: Decimal(\'40800000000.00000000000000\') as \'4.08E+10\'? I\'ve tried this: >>> \'%E\' % Decimal(\'40800000000.00000000000000\') \'4.080000E+10\' But it has those extra 0\'s. 回答1: from decimal import Decimal '%.2E' % Decimal('40800000000.00000000000000') # returns '4.08E+10' In your '40800000000.00000000000000' there are many more significant zeros that have the same meaning as any other digit. That's why you have to tell explicitly where you want to stop. If you want

Using String Format to show decimal up to 2 places or simple integer

隐身守侯 提交于 2019-11-26 00:36:38
问题 I have got a price field to display which sometimes can be either 100 or 100.99 or 100.9, What I want is to display the price in 2 decimal places only if the decimals are entered for that price , for instance if its 100 so it should only show 100 not 100.00 and if the price is 100.2 it should display 100.20 similarly for 100.22 should be same . I googled and came across some examples but they didn\'t match exactly what i wanted : // just two decimal places String.Format(\"{0:0.00}\", 123.4567

Output in a table format in Java&#39;s System.out

微笑、不失礼 提交于 2019-11-26 00:28:09
问题 I\'m getting results from a database and want to output the data as a table in Java\'s standard output I\'ve tried using \\t but the first column I want is very variable in length. Is there a way to display this in a nice table like output? 回答1: Use System.out.format . You can set lengths of fields like this: System.out.format("%32s%10d%16s", string1, int1, string2); This pads string1 , int1 , and string2 to 32, 10, and 16 characters, respectively. See the Javadocs for java.util.Formatter for

Python&#39;s many ways of string formatting — are the older ones (going to be) deprecated?

好久不见. 提交于 2019-11-25 23:49:18
问题 Python has at least six ways of formatting a string: In [1]: world = \"Earth\" # method 1a In [2]: \"Hello, %s\" % world Out[2]: \'Hello, Earth\' # method 1b In [3]: \"Hello, %(planet)s\" % {\"planet\": world} Out[3]: \'Hello, Earth\' # method 2a In [4]: \"Hello, {0}\".format(world) Out[4]: \'Hello, Earth\' # method 2b In [5]: \"Hello, {planet}\".format(planet=world) Out[5]: \'Hello, Earth\' # method 2c In [6]: f\"Hello, {world}\" Out[6]: \'Hello, Earth\' In [7]: from string import Template #