首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

Bash字符串处置(与Java对照) - 18.格式化字符串

2012-08-14 
Bash字符串处理(与Java对照) - 18.格式化字符串Bash字符串处理(与Java对照) - 18.格式化字符串In Javaclas

Bash字符串处理(与Java对照) - 18.格式化字符串
Bash字符串处理(与Java对照) - 18.格式化字符串In Javaclass 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

?

\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
?

[root@jfht ~]# ./table.sh
?Triangle?? 00000013??????? red?????? 20.00
?Oval?????? 00204449? dark blue?????? 65.66
?Square???? 00003145???? orange??????? 0.70
[root@jfht ~]#

?

?

本文链接:http://codingstandards.iteye.com/blog/1198098? (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)?

上节内容:Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾

下节内容:Bash字符串处理(与Java对照) - 19.查找字符的位置

?

热点排行