使用“?”格式化字符串的方法java中完整字符串的参数?

发布于 2024-12-02 12:04:50 字数 344 浏览 0 评论 0原文

例如,我想用方法实现类

public class Logger {

    public void info(String message, String[] params) {
    }
}

如果输入是

new Logger().info("Info: param1 is ? , param2 is ?", new  String[] {"a", "b"});

输出必须是

Info: param1 is a , param2 is b

什么是最简单的实现方法?

For example I want to implement class with method

public class Logger {

    public void info(String message, String[] params) {
    }
}

If input is

new Logger().info("Info: param1 is ? , param2 is ?", new  String[] {"a", "b"});

Output must be

Info: param1 is a , param2 is b

What is the easiest way to implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我也只是我 2024-12-09 12:04:51

以下代码将按照您的要求进行操作。

public class Logger {

    public static void main(String[] args) {
        new Logger().info("Info: param1 is ?, param2 is ?", new String[] { "a", "b" });
        new Logger().info("Info: param1 is ?, param2 is ?", "a", "b");
    }

    public void info(final String message, final String... args) {
        System.out.printf(message.replace("?", "%s") + "%n", args);
    }

}

但是,您应该考虑使用正确的格式占位符而不是“?”。例如:

 public class Logger {

    public static void main(String[] args) {
        new Logger().info("Info: param1 is %s, param2 is %s", new String[] { "a", "b" });
        new Logger().info("Info: param1 is %s, param2 is %s", "a", "b");
    }

    public void info(final String message, final String... args) {
        System.out.printf(message + "%n", args);
    }

}

两个版本都会打印:

Info: param1 is a, param2 is b
Info: param1 is a, param2 is b

The following code does as you require.

public class Logger {

    public static void main(String[] args) {
        new Logger().info("Info: param1 is ?, param2 is ?", new String[] { "a", "b" });
        new Logger().info("Info: param1 is ?, param2 is ?", "a", "b");
    }

    public void info(final String message, final String... args) {
        System.out.printf(message.replace("?", "%s") + "%n", args);
    }

}

However, you should consider using correct formatting place-holders instead of '?'. For instance:

 public class Logger {

    public static void main(String[] args) {
        new Logger().info("Info: param1 is %s, param2 is %s", new String[] { "a", "b" });
        new Logger().info("Info: param1 is %s, param2 is %s", "a", "b");
    }

    public void info(final String message, final String... args) {
        System.out.printf(message + "%n", args);
    }

}

Both versions will print:

Info: param1 is a, param2 is b
Info: param1 is a, param2 is b
笑看君怀她人 2024-12-09 12:04:51

如果您不必担心转义 '?',并且无法使用 printf() 样式格式字符串,则可以使用:

public void info(String message, String[] params) {
    System.out.println(
        String.format(message.replace("?", "%s"), params)
    );
}

像这样:

info("apple is to ? as eve is to ?",
    new String[] { "banana", "noon" });

Which prints: 苹果之于香蕉就像夏娃之于中午


如果您需要更复杂的逻辑(例如转义),那么您可能需要基于正则表达式模式的成熟搜索和替换。

If you don't have to worry about escaping the '?', and you can't use printf() style format strings, you could get away with:

public void info(String message, String[] params) {
    System.out.println(
        String.format(message.replace("?", "%s"), params)
    );
}

Like so:

info("apple is to ? as eve is to ?",
    new String[] { "banana", "noon" });

Which prints: apple is to banana as eve is to noon


If you need more sophisticated logic, such as escaping, then you'll probably want a full-blown regexp Pattern-based search and replace.

最冷一天 2024-12-09 12:04:51

在 params[] 的循环中使用正则表达式查找每个“?”在您的字符串中并将其替换为当前参数。谨防空值并找到合适的逃生方法? (猜猜\?会带来一些麻烦,更好吗??)

in a loop on params[] use regexp to find every '?' in your string and substitute it with the current param. Beware of nulls and find a proper way to escape ? (guess \? will give some trouble, better ??)

征棹 2024-12-09 12:04:50

您可以使用 String.format(String format, Object ... args) 方法。您可以使用 C 风格的 %x 格式来代替使用 ?,其中 x 可以是 d (例如int)、s(字符串)等

示例< /a>.

另外,您还可以查看 Formatter.format 类方法。它显示了所有可接受的格式化标志(String.format() 方法使用 Formatter 进行格式化)。

You can use the String.format(String format, Object ... args) method for this. Instead of using a ?, you can do C style %x format, where x can be d (for int), s (for string), etc.

Example.

Also, you can view the Formatter.format class method. It shows you all formatting flags acceptale for formatting (String.format() method uses Formatter to do the formatting).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文