From: http://codingstandards.iteye.com/blog/1198098

In Java

class Formatter

参见:http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax

String.format

static String     format(String format, Object... args)
          使用指定的格式字符串和参数返回一个格式化字符串。

参见:String.format函数使用方法介绍 http://blog.csdn.net/andycpp/article/details/1749700

System.out.printf

参见:http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm

1.  System.out.printf('%b', String str )
2.  System.out.printf('%c', char ch )
3.  System.out.printf('%03d', int i )
4.  System.out.printf('%e', float )
5.  System.out.printf('%03f', float f)
6.  System.out.printf('%.2f', float f )
7.  System.out.printf('{%07.3f}', float f )
8.  System.out.printf('%f', float f )
9.  System.out.printf('%g', float f )
10.  System.out.printf('%h', float f )
11.  System.out.printf('%s', 5)
12.  System.out.printf('%s://%s/%s\n', String str1, String str2, String str3)
13.  System.out.printf('%1 s...', String str )
14.  System.out.printf('%5s', String str)
15.  System.out.printf('%-5s', String str) (2)
16.  System.out.printf( '%-10.10s %s', String word, int length )
17.  System.out.printf('%.5s', String str) (3)
18.  System.out.printf('%s', Date date )
19.  System.out.printf('%tc', Date date ) (lowercase t, lowercase c)
20.  System.out.printf('%tC', Date date ) (lowercase t, uppercase C)
21.  System.out.printf('%tD', Date date )
22.  System.out.printf('%tF', Date date )
23.  System.out.printf('%tr', Date date )
24.  System.out.printf('%tR',Date date )
25.  System.out.printf('%tT', Date date )
26.  System.out.printf('%tz', Date date )
27.  System.out.printf('%Tc', Date date ) (Uppercase T, lowercase c)
28.  System.out.printf('%1x, %1X', 0xCAFE )
29.  System.out.printf( Locale.CHINA, '%tc', Date date )
30.  System.out.printf( Locale.ITALIAN, '%tc', Date date )

In Bash

printf

man bash 写道
printf [-v var] format [arguments]
Write the formatted arguments to the standard output under the control of the format. The format is a
character string which contains three types of objects: plain characters, which are simply copied to
standard output, character escape sequences, which are converted and copied to the standard output, and
format specifications, each of which causes printing of the next successive argument. In addition to
the standard printf(1) formats, %b causes printf to expand backslash escape sequences in the correspond-
ing argument (except that \c terminates output, backslashes in \', \", and \? are not removed, and octal
escapes beginning with \0 may contain up to four digits), and %q causes printf to output the correspond-
ing argument in a format that can be reused as shell input.

The -v option causes the output to be assigned to the variable var rather than being printed to the
standard output.

The format is reused as necessary to consume all of the arguments. If the format requires more argu-
ments than are supplied, the extra format specifications behave as if a zero value or null string, as
appropriate, had been supplied. The return value is zero on success, non-zero on failure.

man 1 printf 写道
FORMAT controls the output as in C printf. Interpreted sequences are:

转义字符:
\" double quote
\NNN character with octal value NNN (1 to 3 digits)
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\xHH byte with hexadecimal value HH (1 to 2 digits)
\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)
\UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits)

%% a single %
%b ARGUMENT as a string with ‘\’ escapes interpreted,

except that octal escapes are of the form \0 or \0NNN

and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs converted to proper type
first. Variable widths are handled.

如果你想对printf命令更深入的了解,参见 http://wiki.bash-hackers.org/commands/builtin/printf

打印换行

示例来自 http://ss64.com/bash/printf.html

# Use \n to start a new line
$ printf "Two separate\nlines\n"          
Two separate
lines

[root@jfht ~]# printf "Two separate\nlines\n" 
Two separate
lines
[root@jfht ~]# echo "Two separate\nlines\n"      
Two separate\nlines\n
[root@jfht ~]# echo -e "Two separate\nlines\n" 
Two separate
lines

[root@jfht ~]# echo -n -e "Two separate\nlines\n" 
Two separate
lines
[root@jfht ~]#

用0填充(Zero Padding)

技巧来自 Zero Padding in Bash  http://jonathanwagner.net/2007/04/zero-padding-in-bash/

创建从1到31为名的目录

for ((x=1;x<=31;x+=1)); do mkdir $x; done

一位数字前面加0

for ((x=1;x< =31;x+=1)); do mkdir `printf "%02d" $x`; done

例子来自 http://ss64.com/bash/printf.html

# Echo a list of numbers from 1 to 100, adding 3 digits of Zero padding
# so they appear as 001, 002, 003 etc:
$ for ((num=1;num<=100;num+=1)); do echo `printf "%03d" $num`; done

设置打印宽度、用空白填充

示例来自 http://linuxconfig.org/bash-printf-syntax-basics-with-examples

#!/bin/bashdivider===============================
divider=$divider$dividerheader="\n %-10s %8s %10s %11s\n"
format=" %-10s %08d %10s %11.2f\n"width=43printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"printf "%$width.${width}s\n" "$divider"printf "$format" \
Triangle 13  red 20 \
Oval 204449 "dark blue" 65.656 \
Square 3145 orange .7
[zcm@bash #86]$./a.shITEM NAME   ITEM ID      COLOR       PRICE
===========================================Triangle   00000013        red       20.00Oval       00204449  dark blue       65.66Square     00003145     orange        0.70
[zcm@bash #87]$

Bash字符串处理(与Java对照) - 18.格式化字符串相关推荐

  1. java 按位置格式化字符串_Java字符串格式化,{}占位符根据名字替换实例

    我就废话不多说了,大家还是直接看代码吧~ import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import j ...

  2. java字符串替换 数组,Java工具类-拆分字符串组装数组,替换字符

    Java工具类--拆分字符串组装数组,替换字符 >>>>>>>>>>>>>>>>>>> ...

  3. python中格式化字符串的使用_Python中的格式化字符串

    Python中的格式化字符串方法: 在学习了一段时间的Python之后,回过头来看书的时候,发现Python中最简单的关于格式化输出字符串的方法也有多种,而这并不是类似于茴香豆的"茴&quo ...

  4. java处理json字符串_常见java对象转换为json字符串处理!!!

    1.JSON简介 1. 概念: JavaScript Object NotationJavaScript对象表示法 Person p = new Person(); p.setName("张 ...

  5. golang 格式化字符串_如何在Go中格式化字符串

    golang 格式化字符串 As strings are often made up of written text, there are many instances when we may wan ...

  6. c语言实现将字符串首尾*删除,java 编写函数将字符串的首尾空格删除。

    String 类有个方法去除字符串首位空格: str.trim(); 查看源代码: public String trim() { int len = value.length; int st = ; ...

  7. Java中日期格式化字符串大小写区别YYYY和yyyy

    Java里面: HH代表24小时制的: hh代表12小时制: MM代表月: mm代表分: DD代表 day of year  今年的第N天,dd是这个月的多少天: yyyy代表year, YYYY代表 ...

  8. android 字符串 转公式,java – 在android中将字符串转换为bigdecimal

    嗨我怎么能在android中将字符串转换为bigdecimal. 这是我的第一项活动: public class ViewCartActivity extends Activity { String ...

  9. java 字符串xml,解析java中的xml字符串?

    how do you parse xml stored in a java string object? Java's XMLReader only parses XML documents from ...

最新文章

  1. 清华大学唐杰教授:人工智能的十年总结
  2. 美日两位科学家获2018年度诺贝尔生理或医学奖
  3. 任正非迷茫的背后是华为在“治未病”
  4. 使用getopts处理输入参数
  5. MyEclipse运行tomcat提示严重错误 严重: Error starting endpoint java.lang.Exception: Socket
  6. jsp/servlet学习笔记(核心编程)mysql部分
  7. 陶哲轩实分析 定理 8.2.2 (无限和的富比尼定理) 证明
  8. adnroidstudio debug手机就自动退出程序_苹果官方表示 iPhone关闭后台程序或将缩短电池寿命...
  9. node2vec: Scalable Feature Learning for networks
  10. 机器学习基础-多项式回归-03
  11. 在Kubernetes中使用Sateful Set部署Redis
  12. jquery验证手机号码和邮箱地址例子
  13. jdbc和mysql编程_MySql数据库与JDBC编程三
  14. Win10 iot 配置防火墙限制应用部署
  15. Python中list,tuple,dict,set的区别和用法
  16. 项目后台运行关闭_iOS到底有没有必要上滑强制关闭APP?
  17. JS内置对象方法——array
  18. 【转-整理】Struts2中package,action,result,method配置详解
  19. 超酷Loading进度条
  20. HTML + CSS + JavaScript 两小时快速入门教程

热门文章

  1. 如何评价强gis与弱gis_什么是gis的简化解释
  2. 莫烦Matplotlib可视化第二章基本使用代码学习
  3. 598. 范围求和 II
  4. leetcode40. 组合总和 II(回溯)
  5. css网格_我如何记住CSS网格属性
  6. htt://3g.hn_根据我对“询问HN:谁在招聘?”的分析,开发人员技能发展趋势
  7. Oracle数据库中游标的游标的使用
  8. Linux 命令行输入
  9. JQuery--事件
  10. PAT 1013 数素数 (20)