java字符串格式化函数,Java字符串format()

本文概述

Java字符串format()方法通过给定的语言环境, 格式和参数返回格式化的字符串。

如果未在String.format()方法中指定语言环境, 则它将通过调用Locale.getDefault()方法来使用默认语言环境。

Java语言的format()方法类似于c语言中的sprintf()函数和Java语言的printf()方法。

内部实施

public static String format(String format, Object... args) {

return new Formatter().format(format, args).toString();

}

签名

字符串format()方法有两种类型:

public static String format(String format, Object... args)

and, public static String format(Locale locale, String format, Object... args)

参量

locale:指定要在format()方法上应用的语言环境。

format:字符串的格式。

args:格式字符串的参数。可能为零或更大。

退货

格式化字符串

投掷

NullPointerException:如果format为null。

IllegalFormatException:如果格式非法或不兼容。

Java String format()方法示例

public class FormatExample{

public static void main(String args[]){

String name="sonoo";

String sf1=String.format("name is %s", name);

String sf2=String.format("value is %f", 32.33434);

String sf3=String.format("value is %32.12f", 32.33434);//returns 12 char fractional part filling with 0

System.out.println(sf1);

System.out.println(sf2);

System.out.println(sf3);

}}

立即测试

name is sonoo

value is 32.334340

value is 32.334340000000

Java字符串格式说明符

在这里, 我们提供了Java字符串支持的格式说明符表。

格式说明符

数据类型

输出量

%a

floating point (except BigDecimal)

返回浮点数的十六进制输出。

%b

Any type

如果为非null, 则为“ true”;如果为null, 则为“ false”

%c

character

Unicode字符

%d

integer (incl. byte, short, int, long, bigint)

Decimal Integer

%e

floating point

科学计数法中的十进制数

%f

floating point

decimal number

%g

floating point

十进制数, 可能以科学计数法表示, 取决于精度和值。

%h

any type

十六进制hashCode()方法中的值的字符串。

%n

none

特定于平台的行分隔符。

%o

integer (incl. byte, short, int, long, bigint)

八进制数

%s

任意种类

String value

%t

日期/时间(包括长, 日历, 日期和TemporalAccessor)

%t是日期/时间转换的前缀。之后, 需要更多的格式化标志。请参阅下面的日期/时间转换。

%x

integer (incl. byte, short, int, long, bigint)

十六进制字符串。

Java String format()方法示例2

此方法支持各种数据类型并将其格式化为字符串类型。让我们来看一个例子。

public class FormatExample2 {

public static void main(String[] args) {

String str1 = String.format("%d", 101); // Integer value

String str2 = String.format("%s", "Amar Singh"); // String value

String str3 = String.format("%f", 101.00); // Float value

String str4 = String.format("%x", 101); // Hexadecimal value

String str5 = String.format("%c", 'c'); // Char value

System.out.println(str1);

System.out.println(str2);

System.out.println(str3);

System.out.println(str4);

System.out.println(str5);

}

}

立即测试

101

Amar Singh

101.000000

65

c

Java String format()方法示例3

除了格式化外, 我们还可以设置任何值的宽度, 填充等。让我们看一个为整数设置宽度和填充的示例。

public class FormatExample3 {

public static void main(String[] args) {

String str1 = String.format("%d", 101);

String str2 = String.format("|%10d|", 101); // Specifying length of integer

String str3 = String.format("|%-10d|", 101); // Left-justifying within the specified width

String str4 = String.format("|% d|", 101);

String str5 = String.format("|%010d|", 101); // Filling with zeroes

System.out.println(str1);

System.out.println(str2);

System.out.println(str3);

System.out.println(str4);

System.out.println(str5);

}

}

立即测试

101

| 101|

|101 |

| 101|

|0000000101|

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值