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()的更多相关文章

  1. C# string.Format 和 String.Format 的区别

    string.Format 和 String.Format  ,不论是用法还是意思,都是一样的 怎么使用? 通过 占位符来替换 ,类似于 Replace 的操作 string s = string.F ...

  2. 点滴拾遗 - 自定义 Format 控制 String.Format 行为

    点击下载示例代码 String.Format 一重载方法的签名如下 public static string Format( IFormatProvider provider, string form ...

  3. JS字符串格式化函数 string.format

    原生JS写的仿C#的字符串format函数,在此基础上又增加了便于JS使用的字面量对象参数. 参照C#中的规则,调用的时候会检测字符串格式,如果字符串格式不规范,或者传入的参数为null或undefi ...

  4. String.Format in javascript

    有些时候,我们确实需要在JavaScript中进行字符串替换,类似于C#中的String.Format()方法一样,只不过这种格式化替换只局限于对由'{0}','{1}','{2}'...所组成的“占 ...

  5. (转)使用string.Format需要注意的一个性能问题

    今天,我在写C#代码时,突然发现一个最熟悉的陌生人 —— string.Format.在写C#代码的日子里,与它朝夕相伴,却没有真正去了解它.只知道在字符串比较多时,用它比用加号进行字符串连接效率更高 ...

  6. 异常-----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 ...

  7. Java中利用MessageFormat对象实现类似C# string.Format方法格式化

    我们在写C#代码的时候常常会使用到string.Format("待格式化字符串{0},{1},....",参数1,参数2,...),来格式化字符串,特别是拼接字符的时候,这种方式使 ...

  8. String.format和MessageFormat.format的对比用法

    1.MessageFormat.format import java.text.MessageFormat; /** * Created by SYJ on 2017/9/13. */ public ...

  9. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

随机推荐

  1. mysql 添加外键

    create table class( cid tinyint unsigned primary key auto_increment, caption varchar(15) not null)en ...

  2. 3A - Holding Bin-Laden Captive!

    We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But rec ...

  3. Ionic3--数据存储

    1.使用sqlite cordova plugin add cordova-sqlite-storage --save npm install --save @ionic/storage (本地存储) ...

  4. java有车有房有能力最基本运用

    public class yunsuan { public static void main(String[] args) { // 1是有,0是没有 int i = 1, l = 0;// 有房 i ...

  5. Android.Tools.Summary

    Android平台上工具的总结 每个工具的详细使用和深入理解参考每个工具相关的blog. 1. Android SDK中提供的工具 http://developer.android.com/tools ...

  6. Java中关键字static的使用

    static 关键字 1).static只能修饰成员变量或成员方法,所有非静态是对象相关的,所有静态是类相关的. 2)被static修饰的成员变量成员方法独立于该类的任何对象,它不依赖类的特定的实例, ...

  7. python httplib2应用get post

    import httplib2,time #装饰器方法,用于记录方法消耗时间 #推荐将print 改成log def timer(func):     def _warpper(self,*argv) ...

  8. BUG(1):一个关于指针的bug

    是时候记录一下这个让我栽了两次的bug了. 具体情况如下: #include <stdio.h>#include <stdlib.h> struct app_info_t { ...

  9. Linux配置nodejs

    http://my.oschina.net/blogshi/blog/260953 首先去官网下载代码,这里一定要注意安装分两种,一种是Source Code源码,一种是编译后的文件.我就是按照网上源 ...

  10. [Hbase]Hbase章2 Hbase读写过程解析

    写数据 Hbase使用memstore和storefile存储对表的更新.数据在更新时首先写入hlog和memstore,memstore中的数据是排序的,当memstore累计到一定的阀值时,就会创 ...