日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/9 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bash字符串处理(与Java对照) - 18.格式化字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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.格式化字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。