【问题标题】:Java String Padding with spaces [duplicate]带有空格的Java字符串填充[重复]
【发布时间】:2012-07-21 17:00:24
【问题描述】:

朋友们,我在项目中要暗示一些东西,我发现了一些困难,如下:

String name1 = "Bharath"  // its length should be 12
String name2 = "Raju"     //  its length should be 8
String name3 = "Rohan"    //  its length should be 9
String name4 = "Sujeeth"   //  its length should be 12
String name5 = "Rahul"  //  its length should be 11  " Means all Strings with Variable length"

我有字符串和它们的长度。我需要得到如下格式的输出。通过使用字符串连接和填充。我需要在 Groovy 中回答,即使 Java 也很好..

"Bharath     Raju    Rohan    Sujeeth     Rahul     "

意思是:

Bharath 向前 5 个黑色空间,因为 lenth 为 12 (7+5 = 12),

Raju 向前 4 个黑色空格,因为 lenth 为 8 (4+4 = 8),

Rohan 开始 4 个黑色空格,因为 lenth 是 9(5+4),

Sujeeth 向前 5 个黑色空格,因为 lenth 是 12 (7+5),

Rahul 向前 6 个黑色空格,长度为 11(5+6),

【问题讨论】:

  • 空格是看不到有问题的。我在问题中已经解释过了。请任何人帮助我。
  • 尝试使用java.util.FormatterString.format() 来实现。

标签: java string groovy


【解决方案1】:

您可以使用sprintf,它已添加到 Object 类中,因此始终可用:

def s = sprintf("%-12s %-8s %-9s %-12s %-11s", name1, name2, name3, name4, name5)

assert s == "Bharath      Raju     Rohan     Sujeeth      Rahul      "

sprintf 使用的格式字符串与Formatter 类使用的格式字符串相同。请参阅JDK documentation for the format string 了解更多信息。

【讨论】:

    【解决方案2】:

    如前所述,您可以使用 String.format() 方法来实现您的目标。

    例如:

        String[] strings = {
                "toto1",
                "toto22",
                "toto333",
                "toto",
                "totoaa",
                "totobbb",
                "totocccc",
        };
        String marker = "01234567890|";
        String output = "";
        for(String s : strings) {
            output += marker;
        }
        System.out.println(output);
        output = "";
        for(String s : strings) {
            output += String.format("%-12s", s);
        }
        System.out.println(output);
    

    这将输出带有 12 个字符的标记的第一行,然后输出带有预期字符串的第二行:

    01234567890|01234567890|01234567890|01234567890|01234567890|01234567890|01234567890|
    toto1       toto22      toto333     toto        totoaa      totobbb     totocccc    
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      // A list of names
      def names = [ "Bharath", "Raju", "Rohan", "Sujeeth", "Rahul" ]
      
      // A list of column widths:
      def widths = [ 12, 8, 9, 12, 11 ]
      
      String output = [names,widths].transpose().collect { name, width ->
        name.padRight( width )
      }.join()
      

      这使得output 等于:

      'Bharath     Raju    Rohan    Sujeeth     Rahul      '
      

      假设我理解了这个问题...很难确定...

      【讨论】:

        【解决方案4】:

        看看 Apache 的StringUtils。它有用空格填充的方法(左或右)。

        【讨论】:

        • 如果您使用的是 Groovy,则无需其他依赖项......它已经拥有 padLeftpadRight metaClass methods for String
        • @tim_yates:我通常发现 Util 类非常方便,并会推荐它们。话虽这么说,但如果只是为了这个操作,我同意你避免额外的依赖。
        猜你喜欢
        • 2021-05-20
        • 2014-03-16
        • 1970-01-01
        • 2012-10-11
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        相关资源
        最近更新 更多