CodeGym /ื‘ืœื•ื’ Java /Random-HE /ืคื•ืจืžื˜ Java String()
John Squirrels
ืจึธืžึธื”
San Francisco

ืคื•ืจืžื˜ Java String()

ืคื•ืจืกื ื‘ืงื‘ื•ืฆื”

ืžื”ื™ ืฉื™ื˜ืช Java string.format()?

ืฉื™ื˜ืช Java string format() ืžืฉืžืฉืช ืœืขื™ืฆื•ื‘ ืžื—ืจื•ื–ื•ืช, ืžืกืคืจื™ื ืฉืœืžื™ื, ืขืจื›ื™ื ืขืฉืจื•ื ื™ื™ื ื•ื›ืŸ ื”ืœืื”, ืขืœ ื™ื“ื™ ืฉื™ืžื•ืฉ ื‘ืžืคืจื˜ื™ ืคื•ืจืžื˜ื™ื ืฉื•ื ื™ื. ืฉื™ื˜ื” ื–ื• ืžื—ื–ื™ืจื” ืืช ื”ืžื—ืจื•ื–ืช ื”ืžืขื•ืฆื‘ืช ื‘ืืžืฆืขื•ืช ื”ืžืงื•ื ื”ื ืชื•ืŸ, ื”ืคื•ืจืžื˜ ืฉืฆื•ื™ืŸ ื•ื”ืืจื’ื•ืžื ื˜ื™ื. ืื ืœื ืžืกื•ืคืง ืžืงื•ื, ื”ื•ื ืžืฉืชืžืฉ ื‘ื›ืชื•ื‘ืช ื‘ืจื™ืจืช ื”ืžื—ื“ืœ ืœืขื™ืฆื•ื‘ ื”ืžื—ืจื•ื–ื•ืช. ื”- string.format() ื”ื™ื ื”ืฉื™ื˜ื” ื”ืกื˜ื˜ื™ืช ืฉืœ ื”ืžื—ืœืงื” Java String . ืคื•ืจืžื˜ Java String() - 1ืชื—ื‘ื™ืจ ื™ืฉื ื ืฉื ื™ ืกื•ื’ื™ื ืฉืœ ืฉื™ื˜ื•ืช ืชื‘ื ื™ืช ืžื—ืจื•ื–ืช () . ืื—ื“ ืขื ืžื™ืงื•ื ืžืกื•ืคืง ื•ื”ืฉื ื™ ื‘ืœืขื“ื™ื•, ื”ืžืฉืชืžืฉ ื‘-local ื‘ืจื™ืจืช ื”ืžื—ื“ืœ.
public static String format(Locale loc, String format, Objectโ€ฆ args)
public static String format(String format, Objectโ€ฆ args)
ืคืจืžื˜ืจื™ื
  1. ืขืจืš ื”ืžืงื•ื ืฉื™ื™ื•ืฉื ื‘ืฉื™ื˜ืช format() .
  2. ืฆื™ื•ืŸ ื”ืคื•ืจืžื˜ ืฉืœ ืžื—ืจื•ื–ืช ื”ืคืœื˜.
  3. ืžืกืคืจ ื”ืืจื’ื•ืžื ื˜ื™ื ืขื‘ื•ืจ ืžื—ืจื•ื–ืช ื”ืคื•ืจืžื˜ ื ืข ื‘ื™ืŸ 0 ืœืจื‘ื™ื.
ืžื—ื–ื™ืจ ื–ื” ืชืžื™ื“ ืžื—ื–ื™ืจ ืืช ื”ืžื—ืจื•ื–ืช ื”ืžืขื•ืฆื‘ืช ื‘ื”ืชืื ืœืžืงื•ื. ื—ืจื™ื’ื™ื ื”ืฉื™ื˜ื” format () ืžื—ื–ื™ืจื” 2 ื—ืจื™ื’ื™ื.
  1. NullPointerException , ืื ื”ืคื•ืจืžื˜ ื”ื•ื null ืื– NullPointerException ื ื–ืจืง.
  2. IllegalFormatException , ืื ื”ืคื•ืจืžื˜ ืฉืฆื•ื™ืŸ ืื™ื ื• ื—ื•ืงื™, ืื• ืฉืœื ืกื•ืคืงื• ืืจื’ื•ืžื ื˜ื™ื ืžืกืคื™ืงื™ื, ื”ื—ืจื™ื’ ื”ื–ื” ื ื–ืจืง.

ืžืคืจื˜ื™ ืคื•ืจืžื˜

ื”ื‘ื” ื ืกืชื›ืœ ืขืœ ื›ืžื” ืžืคืจื˜ื™ื ื ืคื•ืฆื™ื.
ืžืคืจื˜ ืชื™ืื•ืจ
%s, %S ืžืขืฆื‘ ืžื—ืจื•ื–ืช.
%d ืžืกืคืจ ืฉืœื ืขืฉืจื•ื ื™, ื”ืžืฉืžืฉ ืขื‘ื•ืจ ืžืกืคืจื™ื ืฉืœืžื™ื ื‘ืœื‘ื“.
%o ืžืกืคืจ ืฉืœื ืื•ืงื˜ืœื™, ื”ืžืฉืžืฉ ืขื‘ื•ืจ ืžืกืคืจื™ื ืฉืœืžื™ื ื‘ืœื‘ื“.
%f, %F ืขื‘ื•ืจ ืžืกืคืจื™ื ืขืฉืจื•ื ื™ื™ื, ืžืฉืžืฉ ืœืžืกืคืจื™ ื ืงื•ื“ื” ืฆืคื”.
%x, %X ืžืกืคืจ ืฉืœื ื”ืงืกื“ืฆื™ืžืœื™, ื”ืžืฉืžืฉ ืขื‘ื•ืจ ืžืกืคืจื™ื ืฉืœืžื™ื ื‘ืœื‘ื“.
ื‘ื•ืื• ื ืกืชื›ืœ ืขืœ ืžืคืจื˜ื™ื ืืœื” ืขื ื“ื•ื’ืžืื•ืช.

ืฉื™ื˜ื•ืช Java String.format() ื“ื•ื’ืžืื•ืช

class Main {
    public static void main(String[] args) {
        // Integer value
        System.out.println(String.format("%d", 234));
        // String value
        System.out.println(String.format("%s", "format() method"));
        // Float value
        System.out.println(String.format("%f", 99.99));
        // Hexadecimal value
        System.out.println(String.format("%x", 99));
        // Char value
        System.out.println(String.format("%c", 'f'));
        // Octal value
        System.out.println(String.format("%o", 99));
    }
}
ืชึฐืคื•ึผืงึธื”
ืฉื™ื˜ืช 234 format() 99.990000 63 f 143
ื“ื•ื’ืžื
class Main {
  public static void main(String[] args) {
    int n1 = 99;

    // using two different specifiers for formatting the string
    System.out.println(String.format("%s\nhexadecimal: %x", "Result is", n1));
  }
}
ืชึฐืคื•ึผืงึธื”
ื”ืชื•ืฆืื” ื”ื™ื ื”ืงืกื“ืฆื™ืžืœื™ืช: 63
ื“ื•ื’ืžื
// to use Locale
import java.util.Locale;

class Main {
  public static void main(String[] args) {
    int number = 9999999;

    // using the default locale if none specified
    System.out.println(String.format("Number: %,d", number););

    // using the GERMAN locale as the first argument
    System.out.println(String.format(Locale.GERMAN, "Number in German: %,d", number));
  }
}
ืชึฐืคื•ึผืงึธื”
ืžืกืคืจ: 9,999,999 ืžืกืคืจ ื‘ื’ืจืžื ื™ืช: 9.999.999

ืกื™ื›ื•ื

ืื ื• ืžืงื•ื•ื™ื ืฉื‘ื™ื ืชื™ื™ื ืืชื” ืžื‘ื™ืŸ ืžื”ื™ ืฉื™ื˜ืช Java string format() ื•ื›ื™ืฆื“ ืœื™ื™ืฉื ืื•ืชื” ืขื‘ื•ืจ ืžืคืจื˜ื™ ืคื•ืจืžื˜ื™ื ืฉื•ื ื™ื ื›ื“ื™ ืœืงื‘ืœ ืืช ื”ืชื•ืฆืื•ืช ื”ืจืฆื•ื™ื•ืช. ืืœ ืชื”ืกืก ืœื”ืชืืžืŸ ื•ืœื—ื–ื•ืจ ื‘ื›ืœ ืคืขื ืฉืชื–ื“ืงืง ืœืกื™ื•ืข ื ื•ืกืฃ. ืœืžื™ื“ื” ืžื”ื ื”! ื›ื“ื™ ืœื—ื–ืง ืืช ืžื” ืฉืœืžื“ืช, ืื ื• ืžืฆื™ืขื™ื ืœืš ืœืฆืคื•ืช ื‘ืฉื™ืขื•ืจ ื•ื™ื“ืื• ืžืงื•ืจืก Java ืฉืœื ื•
ื”ืขืจื•ืช
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION