如何在使用String.format时打印不带引号的null

huangapple go评论72阅读模式
英文:

How to print null without quotes when using String.format

问题

StringJava.class

public static String myString = "This is my name: %s";

Suppose in my main class I am using String.format() in such a way that if we pass some value it will return the respective string. For example:

System.out.println(String.format(StringJava.myString, some_value));

It will give me the expected result that is: This is my name "some_value".

But if I try to pass null values like this:

System.out.println(String.format(StringJava.myString, null));

It will give me the result that is: This is my name "null".

But I want my result as: This is my name null.

How can I fix this "null" issue?

英文:

I am working on one program in Java that is using String.format() to print a string. Below scenario is:

StringJava.class

public static String myString = "This is my name : "\%s\""

Suppose in my main class I am using String.format() in such a way that if we pass some value it will return the respective string. For example:

System.out.println(String.format(StringJava.myString, some_value))

It will give me the expected result that is : This is my name "some_value"

But if i try to pass null values like this:

System.out.println(String.format(StringJava.myString, null))

It will give me the result that is: This is my name "null"

But I want my result as: This is my name null

How can I fix this "null" issue?

答案1

得分: 3

// 代码部分不要翻译

public static String myString = "This is my name : %s";

System.out.println(String.format(StringJava.myString, wrapWithQuotes("some_value")));
System.out.println(String.format(StringJava.myString, wrapWithQuotes(null)));

private String wrapWithQuotes(String input) {
    return (input != null) ? '"' + input + '"' : null;
}
英文:

Instead of specifying the quotes in the myString itself, write a small utility method to process the value before feeding it to the String.format() method. Within this utility method, you can check for null and treat differently.

public static String myString = "This is my name : %s"

System.out.println(String.format(StringJava.myString, wrapWithQuotes("some_value")));
System.out.println(String.format(StringJava.myString, wrapWithQuotes(null)));

private String wrapWithQuotes(String input) {
    return (input != null) ? '"' + input + '"' : null;
}

答案2

得分: 1

你应该将常量 myString 改为一个方法。

public static String myString(String name) {
    return String.format("This is my name : %s", name == null ? null : "\"" + name + "\"");
}

以及

System.out.println(StringJava.myString("Jhon"));
System.out.println(StringJava.myString(null));

输出

This is my name : "Jhon"
This is my name : null
英文:

You should change the constant myString to a method.

public static String myString(String name) {
    return String.format("This is my name : %s", name == null ? null : "\"" + name + "\"");
}

and

 System.out.println(StringJava.myString("Jhon"));
 System.out.println(StringJava.myString(null));

output

This is my name : "Jhon"
This is my name : null

huangapple
  • 本文由 发表于 2020年8月18日 20:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63468243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定