当前位置:首页 > 开发 > 操作系统 > 正文

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

发表于: 2011-10-17   作者:codingstandards   来源:转载   浏览次数:
摘要: Bash字符串处理(与Java对照) - 18.格式化字符串 In Java class Formatter 参见:http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax   String.format static String     format

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

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/bash

divider===============================
divider=$divider$divider

header="\n %-10s %8s %10s %11s\n"
format=" %-10s %08d %10s %11.2f\n"

width=43

printf "$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.查找字符的位置

 

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
一、概述 Java是以String类型的对象来实现字符串。String是一个类,当创建一个String对象后,所创建
var str = '{"id":12, "name":"张三","data":{"id2":12, "name2":"张三","data":{"id3":12, "name3"
类似于strlen,strcmp,strpcy就不写了。。。。。。以下都是在VC6.0下编译,可能在VS上编译错误。 app
1、实例化String对象 直接赋值(节省内存) 使用关键字new "=="比较的是地址,equals比较的是内容(开
Android字符串资源及其格式化 在Android项目布局中,资源以XML文件的形式存储在res/目录下。为了更
在strings.xml文件中定义: <string name="welcome_messages">你好, %1$s! 是第 %2$d 个人.&l
1) The File class has a deceiving name; you might think it refers to a file, but it doesn’t.
JAVA编解码 ---- 乱码问题 ---- 通过一个事例进行分析 一、需求: 二、过程分析: 第一步:java文件
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method s
JAVA编解码 ---- 乱码问题 ---- 通过一个事例进行分析 一、需求: 二、过程分析: 第一步:java文件
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号