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 ...
随机推荐
- Oracle_SQL(7) 复杂查询
1.rownum 伪列<,<=select * from emp where rownum<5; 取工资前3名的人员 select * from (select * from emp ...
- Oracle 修改字段顺序的两种方法
分类: Oracle 如果要修改字段顺序,一般情况可以使用以下步骤: --(1)备份目标表数据 create table 临时表 as select * from 目标表; --(2)drop 目标表 ...
- Python 官方文件
7.2. 文件读写 函数 open() 返回 文件对象,通常的用法需要两个参数:open(filename, mode). >>> f = open('workfile', 'w') ...
- 关于sublime Text 3安装sublimecodeIntel插件配置方法
打开preferences-package settings-sublimecodeIntel-settings users 添加 { "JavaScript": { " ...
- linux 使用笔记6
---恢复内容开始--- 1.内容追加 把一个文件的内容追加到另一个文件中: cat first.txt >> second.txt//追加到second.txt文件的末端 cat ...
- De novo RNA-Seq Assembly Using De Bruijn Graphs
De novo RNA-Seq Assembly Using De Bruijn Graphs 2017-06-12 09:42:47 59 0 0 在说基因组的拼接之前,可 ...
- jsonp,ajax,json问题
JSONP技术 JSONP是解决跨域问题的一种常见方式 跨域问题,因为浏览器有同源策略,所以当不同域间进行数据交互的时候就会出现跨域问题 同源策略:只有在同协议.同域名.同端口的情况下才能进去数据交互 ...
- djiango控制语句
{# 从0开始的索引#} {% for foo in value %} {# 从0开始的索引#} <p>{{ forloop.counter0 }}: {{ foo }}</p> ...
- PHP 批量移动文件改名
public function changeCoverName(){ //$type = '考研'; //$coverPath = './Public/course_cover/kaoyan/'; $ ...
- Sliding Window Median LT480
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...