%[index$][标识][最小宽度]转换方式

[index$]可以用于表示对第index个参数进行格式化,

 // Java代码
String s1=String.format("%3$s,%2$s,%1$s","a","b","c"); // c,b,a

与C#的索引相比,区别在于C#的索引从0开始,并且是不能省略的

Java的索引是从1开始,并且可以省略

// Java代码
String s1=String.format("%s,%s,%s","a","b","c"); // a,b,c 索引可以省略
System.out.println(s1);
String s2=String.format("%3$s,%2$s,%1$s","a","b","c"); // c,b,a 索引从1开始
System.out.println(s2);
// C#
String result = String.Format("{2},{1},{0}","a","b","c"); // c,b,a 注意索引不能省略,并且从0开始
Console.WriteLine(result);

%s转换方式

其实%s可以转换各种类型,其实就是各种类型的 toString()

// Java代码
String s=String.format("%s,%s,%s,%s,%s","a",1,9.2, LocalDateTime.now(),new Date()); // a,1,9.2,2018-02-03T22:36:01.499,Sat Feb 03 22:36:01 CST 2018
System.out.println(s);

与C#对应的,默认转换方式一样

// C#
String result = String.Format("{0},{1},{2},{3}", "a", , 9.2, DateTime.Now); // a,1,9.2,2018/2/3 22:39:52
Console.WriteLine(result);

.Net转Java.08.format的更多相关文章

  1. java.time.format.DateTimeFormatter

    Java的日期与时间 DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下.   1.预定义的DateTimeFo ...

  2. JAVA Stirng.format 使用理解

    JAVA Stirng.format 使用理解前言:项目中需要对一些字符串处理发现format方法的神奇之处一.api才是王道第一种二参使用①public static String format(S ...

  3. java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10

    java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...

  4. fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10

    本地java类 import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; ...

  5. java String.Format详解

    JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String format, Object... args) 使用指定的语言 ...

  6. JAVA String.format 方法使用介绍

    1.对整数进行格式化:%[index$][标识][最小宽度]转换方式        我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...

  7. JAVA String.format 方法使用介绍<转>

    在JDK1.5中,String类增加了一个非常有用的静态函数format(String  format, Objece...  argues),可以将各类数据格式化为字符串并输出.其中format参数 ...

  8. java MessageFormat.format 用法

    FormatElement: { ArgumentIndex }:是从0开始的入参位置索引. { ArgumentIndex , FormatType } { ArgumentIndex , Form ...

  9. Java String.Format() 方法及参数说明

    转自:https://blueram.iteye.com/blog/441683 JDK1.5中,String类新增了一个很有用的静态方法String.format():format(Locale l ...

随机推荐

  1. Python_冒泡排序

    从小到大的排序:(最前面的数和一步步和后面的数比较,如果大于则交换,如果不大于则继续循环) 方法1: data = [65, 1, 45, 77, 3, 9, 43, 23, 7, 53, 213, ...

  2. js,JQuery实现,带筛选,搜索的select

    代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title& ...

  3. Excel表列名称(给定一个正整数,返回它在 Excel 表中相对应的列名称。)

    例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... 示例 1: 输入: 1 输出: "A ...

  4. Could not find result map XXX 解决办法

    错误定位在xml文件中 resultmap类型不匹配

  5. FZU 2254 英语考试 (最小生成树)

    在过三个礼拜,YellowStar有一场专业英语考试,因此它必须着手开始复习. 这天,YellowStar准备了n个需要背的单词,每个单词的长度均为m. YellowSatr准备采用联想记忆法来背诵这 ...

  6. Linux内核中Makefile、Kconfig和.config的关系(转)

    我们在编译Linux内核时,往往在Linux内核的顶层目录会执行一些命令,这里我以RK3288举例,比如:make firefly-rk3288-linux_defconfig.make menuco ...

  7. switch语句以及三种循环语句的总结

    1:switch语句(1)格式:switch(表达式) {case 值1:语句体1;break;case 值2:语句体2;break;...default:语句体n+1;break;} 格式解释说明: ...

  8. SpringBoot整合elasticsearch

    在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...

  9. 关于GitHub中文乱码的问题

    其实跟Git里中文乱码相比,这个问题好解决得多. GitHub中的乱码是由于其使用的编码与Windows记事本的默认编码不同.只要在本地将编码改成UTF-8,再重新push就可以了. 修改编码的方法是 ...

  10. LeetCode(15. 三数之和)

    问题描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...