首页 > 其他 > 详细

计算每行所占字符个数,并格式化输出

时间:2015-01-04 18:46:32      阅读:232      评论:0      收藏:0      [点我收藏+]
import scala.io.Source

def widthOfLength(s: String) = s.length.toString.length//计算字符串长度的位数,比如长度为:136,则位数为:3(三位数)
if (args.length > 0) {
    val lines = Source.fromFile(args(0)).getLines.toList
    val longestLine = lines.reduceLeft(
        (a, b) => if (a.length > b.length) a else b
    )
    val maxWidth = widthOfLength(longestLine)
    for (line <- lines) {
        val numSpaces = maxWidth - widthOfLength(line)
        val padding = " " * numSpaces
        println(padding + line.length + "|" + line)
    }
}else
    Console.err.println("Please enter filename")

技术分享

计算每行所占字符个数,并格式化输出

原文:http://www.cnblogs.com/ivan0626/p/4201620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!