Sprintf等同于Java [英] Sprintf equivalent in Java

查看:218
本文介绍了Sprintf等同于Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Printf被1.5版本添加到了Java中,但我似乎无法找到如何将输出发送到字符串而不是文件(这是sprintf在C中的做法)。有谁知道如何做到这一点?

解决方案

  //将格式化的字符串存储在'result'中
字符串结果= String.format(%4d,i * j);

//将结果写入标准输出
System.out.println(result);

请参阅 format 及其语法


Printf got added to Java with the 1.5 release but I can't seem to find how to send the output to a string rather than a file (which is what sprintf does in C). Does anyone know how to do this?

解决方案

// Store the formatted string in 'result'
String result = String.format("%4d", i * j);

// Write the result to standard output
System.out.println( result );

See format and its syntax

这篇关于Sprintf等同于Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆