MessageFormat.format()用法

MessageFormat本身与语言环境无关,而与用户提供给MessageFormat的模式和用于已插入参数的子格式模式有关,以生成适用于不同语言环境的消息。

// 1、ArgumentIndex必须是非负整数,它的个数不只限于0到9这10个,它可以用0到9的数字组成,因此可以有好多个
		String msg = "{0}{1}{2}{3}{4}{5}{6}{7}{8}";
		Object[] array = new Object[] { "A", "B", "C", "D", "E", "F", "G", "H",
				"I", };
		String value = MessageFormat.format(msg, array);
		System.out.println(value); // 输出:ABCDEFGHI
		// 格式化字符串时,两个单引号表式一个单引号,单个引号会被忽略,除非中文单引号不会被省略
		String value1 = MessageFormat.format("oh,{0}is 'a' goodmorning", "你好");
		System.out.println(value1);

		// 单引号会使其后面的占位符均失效,导致直接输出占位符
		String value2 = MessageFormat.format("{0},{1}", 1, 2);// 结果12
		String value3 = MessageFormat.format(" '{0},{1}", 1, 2);// 结果{0}{1}
		String value4 = MessageFormat.format(" '{0}',-{1}", 1, 2);// 结果{0}-2
		System.out.println(value2 + value3 + value4);

		// 使用双引号和两个单引号没关系

		String value5 = MessageFormat.format("oh, ' '{0}' ' is a pig",
				"ZhangSan");
		System.out.println(value5); // 输出:oh, 'ZhangSan' is a pig

		// String value6 = MessageFormat.format("oh, "{0}" is a pig",
		// "ZhangSan");
		// System.out.println(value6); // 切记不能同时使用两个双引号

参考:https://blog.csdn.net/qq_36538061/article/details/78506758

猜你喜欢

转载自blog.csdn.net/Hunter2916/article/details/88552554