格式字串

2021-05-28 18:25:05 字數 1422 閱讀 2933

格式字串

生產格式化字串的每個方法都需要格式字串和引數列表。格式字串是乙個string,它可以包含固定文字以及乙個或多個格式說明符。

string. format(string format, object ... args);

格式說明符的語法

常規型別、字串和數值型的格式說明符的語法

%[argument_index][flags][width][.percision]conversion

例如:%1$ 0 10 d 意思是:在引數列表中的乙個引數格式化數字型別,數字

型別範圍為10位,如果不夠10位則在此引數前

補「0「。

string.format("%1$04d", 55);  結果為:0055

用來表示日期和時間的格式說明符語法如下

%[argument_index][flags][width]conversion

conversion 有兩個字元組成的序列。第乙個字元是「t「 或 」t「,第二個是字元表明所使用的格式。

與引數不對應的格式說明符的語法如下

%[flags][width]conversion

所需conversion是乙個表明要在輸出中所插內容的字元。

以下格式化日期為例:

public static voidformatedate(calendar calendar)

public static voidformatedateyear(calendar calendar)

public static voidformatedateandhours(calendar calendar)

public static voidformatedatehours(calendar calendar)

public static voidformatedatemonths(calendar calendar)

/***@paramcalendar

*/public static voidformatedateseconds(calendar calendar)

public static voidformatstring01()

public static voidformatstring02()

/*** 列印輸出

* *@paramo

*/public static voidp(object o) {

system.out.println(o.tostring());

轉換字串格式

轉換字串格式為原來字串裡的字元 該字元連續出現的個數,例如字串 1233422222 轉換為1121324125 1出現1次,2出現1次,3出現2次,4出現1次,2出現5次 解法 可通過sprintf語句,位於標頭檔案中。與printf在用法上幾乎一樣,只是列印的目的地不同而已,前者列印到字串中,後...

pythonformat格式字串

語法 它通過 和 來代替 注意 字串的format函式可以接受無限個引數,位置可以不按順序,可以不用或者用多次,不過2.6不能為空 2.7才可以。通過位置 in 1 format kzc 18 out 1 kzc,18 in 2 format kzc 18 out 2 kzc,18 in 3 for...

json字串格式

res.write 前端轉化 fetch json config then response then data 發現前端報錯 uncaught in promise syntaxerror unexpected token in json at position 1 我乙個以為是fetch沒有配置...