码迷,mamicode.com
首页 > 其他好文 > 详细

Scala format 字符串格式化

时间:2016-11-15 00:23:23      阅读:644      评论:0      收藏:0      [点我收藏+]

标签:turn   eal   第一个   文档   mat   输出   replace   字符串格式化   ring   

A wrapper class that adds a formatted operation to any value
Annotations   @deprecated
Deprecated    (Since version 2.11.0) Use Predef.StringFormat
Source        StringFormat.scala

def formatted(fmtstr: String): String
      Returns string formatted according to given format string. Format strings are as for String.format (@see java.lang.String.format).
     
import scala.runtime.StringFormat
// scala字符串填充
for (x<-1 to 10){
      val str=new StringFormat( x.toString() )
      val s1=str.formatted("%3s").replaceAll(" ", "0")
      println("99"+s1)
    }
   
99001
99002
99003
99004
99005
99006
99007
99008
99009
99010


查看JDK文档得知,String.format方法的第一个参数是有个公式
    %[argument_index$][flags][width][.precision]conversion
argument_index: 可选,表明参数在参数列表中的位置。第一个参数由 "1$" 引用,第二个参数由 "2$" 引用,依此类推。
flags: 可选,用来控制输出格式
width: 可选,是一个正整数,表示输出的最小长度
precision:可选,用来限定输出字符数,精度
conversion:必须,用来表示如何格式化参数的字符

Scala format 字符串格式化

标签:turn   eal   第一个   文档   mat   输出   replace   字符串格式化   ring   

原文地址:http://www.cnblogs.com/wwxbi/p/6063848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!