格式化字符串

	String msgStr="my name is {0} and my age is {1};";

		String result = MessageFormat.format(msgStr,"tom",20);

		System.out.println(result);

		//输出: my name is tom and my age is 20;

		

		/* 若是有单引号,不会做处理,并且已有的单引号也会被去掉 ***/

		 msgStr="my 'name' is '{0}' and my age is '{1}';";

		 result = MessageFormat.format(msgStr,"tom",20);

		 System.out.println(result);

		 //输出: my name is {0} and my age is {1};

		 

		 /* 若是有单引号,做如下处理 ***/

		 msgStr="my ''name'' is ''{0}'' and my age is ''{1}'';";

		 result = MessageFormat.format(msgStr,"tom",20);

		 System.out.println(result);

		 //输出: my 'name' is 'tom' and my age is '20';

		 

		 /* 也可用string.format处理 ***/

		 msgStr="my 'name' is '%s' and my age is '%d';";

		 result = String.format(msgStr,"tom",20);

		 System.out.println(result);

		 //输出: my 'name' is 'tom' and my age is '20';

  

你可能感兴趣的:(字符串)