java中怎样实现象C++中一样使用print输出格式,如print("%ld",i)

goodhome 2003-08-22 02:55:33
java中怎样实现象C++中一样使用print输出格式,如print("%ld",i),print("%6s",str) , 谢谢
...全文
159 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodhome 2003-08-22
  • 打赏
  • 举报
回复
up
jyn01 2003-08-22
  • 打赏
  • 举报
回复
ding
goodhome 2003-08-22
  • 打赏
  • 举报
回复
up
scbb 2003-08-22
  • 打赏
  • 举报
回复
只是一个文书打印,已经定义好了格式,有些位置要填数据(几十项,要用+的话偶觉得很麻烦:)
刚写了一个简单的,凑和着用,有bug或效率问题欢迎指出,都有分:)
public static String replace(String source,String[] args)
{
//if(source == null || args == null ||(args.length != source.
int start = 0;
StringBuffer buffer = new StringBuffer(source);
for(int i=0;i<args.length;i++)
{
int postion = source.indexOf("%",start);
if((postion == -1) || ((postion+1) == source.length()))
{
break;
}
switch(source.charAt(postion + 1))
{
case 's':
buffer.delete(postion,postion + 2);
buffer.insert(postion,args[i]);
break;
}
start = postion + args[i].length();
source = buffer.toString();
}
return source;
}
public static void main(String[] args)
{
System.out.println(replace("abc%s%s123%s",new String[]{"abc","cde","slfjslf"}));
System.out.println();
}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧