MessageFormat.format()和String.format()
MessageFormat 提供了以与语言无关方式生成连接消息的方式。使用此方法构造向终端用户显示的消息。
MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置
String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。
package com.java.test;
import java.text.MessageFormat;
public class TestFormat {
public static void main(String args[]){
String str="今天是{0}年{1}月{2}号,天气{3}";
String str1="今天是%s年%d月%d号,天气%s";
Object[] a={"2018","11","23","晴"};
System.out.println(MessageFormat.format(str,"2018","11","23","晴"));
System.out.println(MessageFormat.format(str,a));
System.out.println(String.format("今天是%s年%d月%d号,天气%s","2018",11,23,"晴"));
System.out.println(String.format(str1,"2018",11,23,"晴"));
}
}
结果:

MessageFormat.format()和String.format()的更多相关文章
- C# string.Format 和 String.Format 的区别
string.Format 和 String.Format ,不论是用法还是意思,都是一样的 怎么使用? 通过 占位符来替换 ,类似于 Replace 的操作 string s = string.F ...
- 点滴拾遗 - 自定义 Format 控制 String.Format 行为
点击下载示例代码 String.Format 一重载方法的签名如下 public static string Format( IFormatProvider provider, string form ...
- JS字符串格式化函数 string.format
原生JS写的仿C#的字符串format函数,在此基础上又增加了便于JS使用的字面量对象参数. 参照C#中的规则,调用的时候会检测字符串格式,如果字符串格式不规范,或者传入的参数为null或undefi ...
- String.Format in javascript
有些时候,我们确实需要在JavaScript中进行字符串替换,类似于C#中的String.Format()方法一样,只不过这种格式化替换只局限于对由'{0}','{1}','{2}'...所组成的“占 ...
- (转)使用string.Format需要注意的一个性能问题
今天,我在写C#代码时,突然发现一个最熟悉的陌生人 —— string.Format.在写C#代码的日子里,与它朝夕相伴,却没有真正去了解它.只知道在字符串比较多时,用它比用加号进行字符串连接效率更高 ...
- 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-
1.错误描述 五月 27, 2014 12:07:05 上午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
我们在写C#代码的时候常常会使用到string.Format("待格式化字符串{0},{1},....",参数1,参数2,...),来格式化字符串,特别是拼接字符的时候,这种方式使 ...
- String.format和MessageFormat.format的对比用法
1.MessageFormat.format import java.text.MessageFormat; /** * Created by SYJ on 2017/9/13. */ public ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
随机推荐
- C# iframe session 丢失
在页面A中使用iframe引用另一站点页面B,但页面B上面的session总是丢失,百度了一下,不用改程序,直接在iis里面操作,解决方法如下 1.打开IIS管理器 inetmgr 2.选择被嵌入if ...
- sublime 注释模版插件DocBlockr的使用
一.gihub地址 https://github.com/spadgos/sublime-jsdocs/ 其中有使用的教程可以参考 二.配置示例 安装教程此处略,请自行查找教程 jsdocs_extr ...
- Cmder安装配置
转: 1)Windows 命令行增强 cmder chocolatey 配置指南 2) Windows必备神器Cmder使用教程 3)Windows上的程序员神器——Cmder 4)Windows命令 ...
- Android.PackageManager
1. Apk的安装和更新过程是怎样的呢? Ref[1] 2. 在安装.apk程序包时, .so是如何选择并安装的? 这里的选择是指,是如何根据CPU_ABI和CPU_ABI2的值来选择合适的.so的. ...
- .net中反射技术的应用
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Ref ...
- Two Sum IV - Input is a BST LT653
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- Python中的实例方法、类方法、静态方法和普通方法
为了辨析它们的差别,写了如下代码: class C: def self_method(self, a): return a @classmethod def class_method(cls, a): ...
- ajax在jQuery中的应用 (1)加载异步数据
- The Django Book(自定义ModelAdmi类)
默认的,管理界面下显示的东西只是 python2:def __unicode__(self): 和 python3:def __str__(self): 中返回的字段内容 想要让它更加的多元化的话 c ...
- spring学习 十八 spring的声明事物
1.编程式事务: 1.1 由程序员编程事务控制代码.commit与rollback都需要程序员决定在哪里调用,例如jdbc中conn.setAutoCimmit(false),conn.commit( ...