string-formatting

Named placeholders in string formatting

Hello! In Python, when formatting string, I can fill placeholders by name rather than by position, like that: print "There's an incorrect value '%(value)s' in column # %(column)d" % \ { 'value': x, 'column': y } I wonder if that is possible in Java (hopefully, without external libraries)? Thanks in advance. ...

Null in String.Format args throws NullReferenceException even arg isn't in resultative string

I have a null in one of arguments in String.Format() so call throws NullReferenceException. Why does check take place even the argument isn't in resultant string? class Foo { public Exception Ex { get; set; } } class Program { public static void Main(string[] args) { var f1 = new Foo() { Ex = new Exception("Whatever...

BASH: How do you "split" the date command?

Cygwin user here (though if there's a suitable solution I will carry it over to K/Ubuntu, which I also use). I have a Welcome message in my .bashrc that looks like the following: SAD=(`date +%A-%B-%d-%Y`) DUB=(`date -u +%k:%M`) printf "Today's Date is: ${SAD}.\n" printf "Dublin time is now ${DUB}. (24-Hour Clock)\n" After numerous at...

Actionscript 3.0 String With Format?

How can i format a string with supplied variables in AS3? //vars var myNumber:Number = 12; var myString:String = "Months"; var myObject:MovieClip = year; //string myString.txt = "One (?) consists of (?) consecutive (?)", string(myObject), string(myNumber), myString; so in the string above, i would like myString to display "One year c...

% string formatting doesn't work in class methods? (ruby)

any idea how I can display headers using % for formatting? I does nothing in class method but works nicely in instance method class Stats attr_accessor :type, :count; def initialize type @type = type @count = 0 end def self.display "%s %4s " % ["header1",'header2'] #puts 'headers' ObjectSpace.each_object(Stats) { |...

Take next parameter as field width in String.Format

In C#, I have a width I want to use for some strings, but I won't know that width until runtime. I'm doing something like this: string.Format("{0, " + digits + "}", value) // prints 123 as " 123" Is there a string formatting directive that lets me specify this without smashing my own format string together like this? I looked arou...

Using string.Format for simple things?

In my early .Net programming days, I used string.Format() only for complex string concatenations, for example to compile strings as Problem with customer order 234 of date 2/2/2002 and payment id 55543. But now I use string.Format for almost every string concatenation I have to do, also simple ones such as prefixing a string with somet...

Does Cocoa’s printf-style formatting not work as expected when using %@ conversion specifier and field width?

My understanding of printf-like format strings is that you can prefix any conversion specifier with a minimum field width. This does not seem to work for Cocoa’s %@ specifier. Example: NSLog(@"'%5@'", @"foo"); NSLog(@"'%5s'", [@"foo" UTF8String]); Output: … 'foo' … ' foo' Is this the intended behavior? ...

Formatting a string in Java using class attributes

I have a class with an attribute and getter method: public Class MyClass { private String myValue = "foo"; public String getMyValue(); } I would like to be able to use the value of foo in a formatted string as such: String someString = "Your value is {myValue}." String result = Formatter.format(someString, new MyClass()); // res...

Please take a stab at this VB.Net Oracle-related sample and help me with String.Format.

If the database is not Oracle, it is MS SQl 2008. My task: if Oracle, add two more parameters when calling a stored proc. Oracle and MSFT stored procs are generated; Oracle ones have 3 extra parameters: Vret_val out number, Vparam2 in out number, Vparam3 in out number, ... the rest (The are not actually named Vparam2 and Vparam3, but...

Objective c formatting string for boolean?

What formatter is used for boolean values? EDIT: Example: NSLog(@" ??", BOOL_VAL);, what is ?? ? ...

Convert integer to equivalent number of blank spaces.

I was wondering what the easiest way is to convert an integer to the equivalent number of blank spaces. I need it for the spaces between nodes when printing a binary search tree. I tried this int position = printNode.getPosition(); String formatter = "%1"+position+"s%2$s\n"; System.out.format(formatter, "", node.element); But ...

How to format a string to camel case in XSLT?

I'm trying to format strings in XSLT that needs to be in camel case to be used appropriately for the application I'm working with. For example: this_text would become ThisText this_long_text would become ThisLongText Is it possible to also set this up where I can send an input to the format so I do not have to recreate the format mult...

Finding character in String in Vector.

Judging from the title, I kinda did my program in a fairly complicated way. BUT! I might as well ask anyway xD This is a simple program I did in response to question 3-3 of Accelerated C++, which is an awesome book in my opinion. I created a vector: vector<string> countEm; That accepts all valid strings. Therefore, I have a vector ...

How would you convert secs to HH:MM:SS format in SQLite?

How would you convert secs to HH:MM:SS format in SQLite? ...

How do I add left-padded zeros to a number in Java?

I have an integer 100, how do I format it to look like 00000100 (always 8 digits long)? ...

How to get all string format parameters

Is there a way to get all format parameters of a string? I have this string: "{0} test {0} test2 {1} test3 {2:####}" The result should be a list: {0} {0} {1} {2:####} Is there any built in functionality in .net that supports this? ...

Output in a table format in Java's System.out

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? ...

str.format() does not work, keyError

The following code raise a keyError exception: addr_list_formatted = [] addr_list_idx = 0 for addr in addr_list: # addr_list is a list addr_list_idx = addr_list_idx + 1 addr_list_formatted.append(""" "{0}" { "gamedir" "str" "address" "{1}" } """.fo...

How to print the sign + of a digit for positive numbers in Python.

Is there a better way to print the + sign of a digit on positive numbers? integer1 = 10 integer2 = 5 sign = '' total = integer1-integer2 if total > 0: sign = '+' print 'Total:'+sign+str(total) 0 should return 0 without +. ...