首頁  >  文章  >  Java  >  sprintf Java

sprintf Java

王林
王林原創
2024-08-30 15:18:36948瀏覽

Java中的String.format()相當於sprintf().String。 format() 方法傳回帶有格式化字串的 String 物件。 java string format() 方法是內建方法,根據傳遞給它的語言環境、格式和參數傳回格式化字串。如果字串中未指定區域設定。 format()方法,透過呼叫Locale.getDefault()來使用預設語言環境。在Java語言中,format()方法類似於c語言中的sprintf()方法。字串。 format 方法可用於將格式化字串指派或儲存到另一個字串。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

字串的語法。 Java 中的 format() 方法

字串 format() 方法根據其接受的參數有兩種風格 –

1.

public static String format(String format, Object... args)
{
// code
}

2.

public static String format(Locale locale, String format, Object... args)
{
// code
}

參數:

  • locale – 這不是選用參數。它指定要應用於 format() 方法的區域設定
  • format – 這不是選用參數。它指定要應用於字串的格式。
  • args – 這是一個選用參數。它指定格式化字串的參數。它可以是零個或多個參數。
  • 傳回值 – 此函數的回傳值是格式化的字串。

Java中String.format()方法的實作

public static String format(String format, Object... args) {
return new Formatter().format( format, args ).toString( );
}

字串的工作。 Java 中的 format() 方法

字串的工作原理。 Java 中的 String.format() 方法Java 中的 format() 方法接受三個參數。假設我們必須在 10 個指定寬度內用零填充來列印數字。這樣我們就可以使用字串了。 format()方法為“String.format(“The number is : %010d”, 13002);”,其中第一個參數是格式字串,第二個參數是物件。 format() 方法傳回字串「The number is: 0000013002」。

sprintf Java 的範例

字串範例。 Java 中的 format() 方法顯示不同的格式指定 –

範例#1

代碼:

package jex;
import java.util.*;
public class Ex {
public static void main( String[] args ) {
// Integer value
String s1 = String.format( "The Integer number is : %d" , 132 );
// Float value
String s2 = String.format( "The Float number is : %f" , 132.00 );
// Hexadecimal value
String s3 = String.format( "The Hexadecimal number is : %x" , 132 );
// Char value
String s4 = String.format( "The Char number is : %c" , 'a');
// String value
String s5 = String.format( "The String number is : %s" , "Hello world" );
System.out.println( s1 );
System.out.println( s2 );
System.out.println( s3 );
System.out.println( s4 );
System.out.println( s5 );
}
}

上述程式碼的輸出是 –

sprintf Java

如上面的程式所示,String. format() 方法用於建立格式化字串。在String.format() 方法中,使用不同的格式指定不同的資料類型,例如%d(整數)、%f(浮點)、%x(十六進位)、%c(字元)和%s (字串)。接下來,列印格式化的字串,如上面的輸出所示。
字串範例。 Java 中的 format() 方法顯示不同寬度的格式說明符 –

範例#2

代碼:

package jex;
import java.util.*;
public class Ex {
public static void main( String[] args ) {
// Filling with zeroes
String s1 = String.format( "*%011d*" , 101 );
// Left-justifying within the specified width
String s2 = String.format( "*%-11d*" , 101 );
String s3 = String.format( "*% d*" , 101 );
// Specifying length of integer
String s4 = String.format( "*%11d*" , 101 );
System.out.println( s1 );
System.out.println( s2 );
System.out.println( s3 );
System.out.println( s4 );
}
}

上述程式碼的輸出是 –

sprintf Java

如上面的程式所示,String. format() 方法用於建立格式化字串。字串。 format() 方法對整數格式說明符使用不同的寬度。接下來,列印不同格式的字串,如上面的輸出所示。

字串範例。 Java 中的 format() 方法顯示指定參數位置 –

範例#3

代碼:

package jex;
import java.util.*;
public class Ex {
public static void main( String[] args ) {
String str1 = "Hello World";
int no = 100;
// Specifying argument positions. The %1$ is for the first argument and the %2$ is for the second argument.
String str2 = String.format( "The String is : %1$s and %1$s. \n And the number is : %2$s" , str1, no );
System.out.println( str2 );
}
}

上述程式碼的輸出是 –

sprintf Java

如上面的程式所示,String. format() 方法用於建立格式化字串。字串。 format() 方法使用字串的參數位置和整數格式說明符。 %1$ 指定第一個參數,%2$ 指定第二個參數,依此類推。接下來,列印不同格式的字串,如上面的輸出所示。

結論

java string format() 方法根據傳遞給它的語言環境、格式和參數傳回一個格式化的字串。 Java中的String.format()相當於sprintf().String。 format 方法可用於將格式化字串指派或儲存到另一個字串。

以上是sprintf Java的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn