【问题标题】:split string by fixed length and fill the free spaces with white spaces按固定长度拆分字符串并用空格填充空闲空间
【发布时间】:2020-04-23 15:53:26
【问题描述】:

我想将字符串拆分成固定长度,例如拆分为20个字符,如果拆分后长度小于20个字符,则用空格填充字符串,最多20个字符。

public static List<String> splitEqually(String text, int size) {

    List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);

    for (int start = 0; start < text.length(); start += size) {
        ret.add(text.substring(start, Math.min(text.length(), start + size)));
    }
    return ret;
}

我用上面的代码按大小分割。

【问题讨论】:

标签: java


【解决方案1】:

此代码将解决您的问题。

public static List<String> splitEqually(String text, int size) {

    List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);
    StringBuilder str1 = new StringBuilder();
    for (int start = 0; start < text.length(); start += size) {
        String temp = text.substring(start, Math.min(text.length(), start + size));
        if (temp.length() == size) {
            ret.add(temp);
            System.out.println(temp.length());
        } else {
            int n = size - temp.length();
            str1.append(temp);
            for (int j =0 ; j< n ; j++){
                str1.append(" ");
            }
            System.out.println(str1.length());
            ret.add(str1.toString());
        }

    }
    return ret;
}

【讨论】:

  • 您不必检查每个索引的大小。只有最后一个子字符串会很小
  • 或者,它给出 35、36 字符串长度
  • 如果长度小于则不填充空白
  • @boycod3 现在检查。我检查了吐出的字符串的长度。
  • @sidgate 由于 Start 按大小递增,我们不知道它何时会进行最后一次检查。所以我们需要在每次迭代中检查大小。
【解决方案2】:

只有最后一个值可能不是给定的大小,所以如果需要,用空格填充它,循环之后。

public static List<String> splitFixedWidth(String text, int width) {
    List<String> ret = new ArrayList<>((text.length() + width - 1) / width);
    for (int start = 0; start < text.length(); start += width) {
        ret.add(text.substring(start, Math.min(text.length(), start + width)));
    }
    if (! ret.isEmpty()) {
        String lastValue = ret.get(ret.size() - 1);
        if (lastValue.length() < width) {
            lastValue += " ".repeat(width - lastValue.length());
            ret.set(ret.size() - 1, lastValue);
        }
    }
    return ret;
}

在 11 以下的 Java 版本中,使用:

        if (lastValue.length() < width) {
            char[] buf = new char[width];
            lastValue.getChars(0, lastValue.length(), buf, 0);
            Arrays.fill(buf, lastValue.length(), width, ' ');
            ret.set(ret.size() - 1, new String(buf));
        }

或者更短但效率更低:

        if (lastValue.length() < width) {
            ret.set(ret.size() - 1, String.format("%-" + width + "s", lastValue));
        }

【讨论】:

  • 重复未定义,您使用的是哪个版本的java?
【解决方案3】:

使用 lambda

String blanks20 = "                    ";
ArrayList<String> fixed20 = list.stream().collect( Collector.of( ArrayList::new,
    (l, s) -> {
        for(int i = 0; ; s = s.substring( 20 )) {
          l.add( (s + blanks20).substring( i, i + 20 ) );
          if( s.length() < 20 )
            break;
        } },
      (l, r) -> { l.addAll( r ); return( l ); } ) );

【讨论】:

    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多