%[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. mysql特殊使用

    1.按照 job 和薪水倒序排序: select ename,job,sal from emp order by job desc,sal desc; 2.substr()截取子串 该函数接收3个参数 ...

  2. input时间表单默认样式修改(input[type="date"])

    一.时间选择的种类: HTML代码:选择日期:<input type="date" value="2018-11-15" /> 选择时间:<i ...

  3. java生成二维码并融合模板工具类

    二维码融合模板 二维码融合图片 import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; i ...

  4. Linux用过的命令集合

    1,查看是否安装过openssl:(openssl version -a)(rpm -qa|grep -i openssl) 2,安装gcc:(yum install gcc-c++) 3,查看主机名 ...

  5. Bomb 数位dp

    ---恢复内容开始--- 不能有49 数位dp模板题: #include<bits/stdc++.h> using namespace std; //input by bxd #defin ...

  6. KMP算法2

    给定一个主串s,一个子串sub,将主串中的所有子串替换成replaceStr,并将最终结果输出来. #include<stdio.h> #include<string.h> # ...

  7. 用Java进行大数处理(BigInteger)-hdu1042

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目描述: 代码实现: import java.util.Scanner; import jav ...

  8. Square Destroyer-POJ 1084 (IDA*)

    Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...

  9. eclipse tomcat报Several ports(8005 8080 8009)端口被占用问题解决方案

    在启动tomcat的时候eclipse突然报错 Several ports (8005,8080,8009) required by Tomcat v6.0 Server at localhost a ...

  10. Django分页(二)

    Django分页(二) 要求 .设定每页显示数据条数 # # .用户输入页码(第一页.第二页...) # # .设定显示多少页号 # # .获取当前数据总条数 # # .根据设定显示多少页号和数据总条 ...