string-formatting

Make String.format(“%s”, arg) display null-valued arguments differently from “null”

*爱你&永不变心* 提交于 2020-05-10 03:39:49
问题 Consider the custom toString() implementation of a bean: @Override public String toString() { String.format("this is %s", this.someField); } This yields this is null if someField is null. Is there a way to override the default null string representation of null-valued arguments to another text, i.e., ? without calling explicitly replaceAll(...) in the toString method? Note : The bean inherits from a superclass that could implement Formattable (http://docs.oracle.com/javase/7/docs/api/java

Log4j Implicit String Formatting

吃可爱长大的小学妹 提交于 2020-04-08 08:48:13
问题 I am using log4j v1.2.14 for logging in my project and I am also using Java 7 String.format() to put variables in my output. Currently I am writing LOGGER.info(String.format("Your var is [%s] and you are [%s]", myVar, myVar1)); Is this really the best way to output strings? I feel that log4j should have this implemented implicitly as below: LOGGER.info("Your var is [%s] and you are [%s]", myVar, myVar1); Have I missed something? Further, are there any Java logging frameworks that support this

JSON Beautifier Library for Java

六月ゝ 毕业季﹏ 提交于 2020-03-18 04:00:33
问题 I want to format a string containing JSON data using Java. Does anybody know an open source library for that. 回答1: Assuming you're starting out with an existing JSON string, then Jackson can do this for you: ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); String originalJson = ... JsonNode tree = objectMapper .readTree(originalJson); String formattedJson = objectMapper.writeValueAsString(tree); 回答2: Update to previous

JSON Beautifier Library for Java

自古美人都是妖i 提交于 2020-03-18 04:00:32
问题 I want to format a string containing JSON data using Java. Does anybody know an open source library for that. 回答1: Assuming you're starting out with an existing JSON string, then Jackson can do this for you: ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); String originalJson = ... JsonNode tree = objectMapper .readTree(originalJson); String formattedJson = objectMapper.writeValueAsString(tree); 回答2: Update to previous

Python __repr__ for all member variables

徘徊边缘 提交于 2020-02-27 02:25:10
问题 Implementing __repr__ for a class Foo with member variables x and y , is there a way to automatically populate the string? Example that does not work: class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})".format(**self.__dict__) >>> foo = Foo(42, 66) >>> print(foo) IndexError: tuple index out of range And another: from pprint import pprint class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})"

Python __repr__ for all member variables

会有一股神秘感。 提交于 2020-02-27 02:24:23
问题 Implementing __repr__ for a class Foo with member variables x and y , is there a way to automatically populate the string? Example that does not work: class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})".format(**self.__dict__) >>> foo = Foo(42, 66) >>> print(foo) IndexError: tuple index out of range And another: from pprint import pprint class Foo(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Foo({})"

Get number of placeholders in Formatter.format() String

早过忘川 提交于 2020-02-04 01:28:15
问题 In a Java program I am using a String with Formatter.format() function, which I get from server. I cannot be sure that String to be formatted has placeholder or a valid number of them. If it the String is not as expected I would like to throw an exception - or log it somehow. At this point I don't care of what type of placeholders are ( String , Integer ,...), I would just like to get number of expected parameters for each String. What is the easiest way to achieve this? One way could be to

How to turn a list/tuple into a space separated string in python using a single line?

寵の児 提交于 2020-02-03 04:46:25
问题 I tried doing: str = "" "".join(map(str, items)) but it says str object is not callable. Is this doable using a single line? 回答1: Use string join() method. List: >>> l = ["a", "b", "c"] >>> " ".join(l) 'a b c' >>> Tuple: >>> t = ("a", "b", "c") >>> " ".join(t) 'a b c' >>> Non-string objects: >>> l = [1,2,3] >>> " ".join([str(i) for i in l]) '1 2 3' >>> " ".join(map(str, l)) '1 2 3' >>> 回答2: The problem is map need function as first argument. Your code str = "" "".join(map(str, items)) Make

Space in StringFormat

拥有回忆 提交于 2020-02-02 03:02:07
问题 I'm looking to add a comma and one 'space' after a binding. I have the comma after the binding but I cannot work out at all how to do a 'space'. This is what I have; <TextBlock Text="{Binding Name, StringFormat={}{0:\,}\,}" /> Could someone please inform me on how to add a 'space' after the comma? I have tried literally adding a space in the XAML but this does not work. 回答1: XAML trims the string of whitespace unless you tell it where the string ends. Surround your string with single quotes.

How printf function handle %f specification?

早过忘川 提交于 2020-01-30 12:28:27
问题 I have a couple of programs whose output I cannot understand: Program 1 #include <stdio.h> int main(void) { int i=1; float k=2; printf("k is %f \n",k); printf("i is %f \n",i); return 0; } Output is at http://codepad.org/ZoYsP6dc k is 2.000000 i is 2.000000 Program 2 Now one more example #include <stdio.h> int main(void) { char i='a'; int a=5; printf("i is %d \n",i); // here %d type cast char value in int printf("a is %f \n",a); // hete %f dont typecast float value printf("a is %f \n",(float)a