java - 通过添加空格使字符串大小相等

标签 java string

我有一个字符串列表。我想让所有字符串的长度相等。 到目前为止我尝试过的是

    int largestLineLength = 0;

    for(int i=0;i<list.size();i++){
        String s = list.get(i);
        list.remove(s)
        list.add(s.trim());
    }
    //get the largest line
    for (String s : list) {
        int length = s.trim().length();
        if (length > largestLineLength) {
            largestLineLength = length;
        }
    }

    for(String s: list){
        while(s.length() != largestLineLength){
            //add spaces to s
        }
    }

要求:

  1. 通过添加空格使所有行的长度相等 词与词之间。
  2. 行前或行后不得添加空格。
  3. 空格应均匀分布,以使所有行相等

例如

Luke Skywalker has returned 
to his home planet of Tatooine 
in order to — okay, you 
know what, we don't care. 
We were thinking of not even 
doing this one. 

应该是

Luke  Skywalker  has  returned 
to his home planet of Tatooine 
in   order  to  —   okay,  you 
know   what,  we  don't  care. 
We  were  thinking of not even 
doing this one. 

附注最后一行是一个异常(exception)

最佳答案

一个简单的解决方案是找到最长行和当前行之间的差异。找出行中的每个单词,然后在每个世界之后分配空格。

我已经实现了这个解决方案,并且可以在线使用here看看我的解决方案:

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();

    list.add("Luke Skywalker has returned");
    list.add("to his home planet of Tatooine");
    list.add("in order to — okay, you");
    list.add("know what, we don't care.");
    list.add("We were thinking of not even");
    list.add("doing this one.");

    int largestLineLength = 0;

    for (String s : list) {
        int length = s.length();
        if (length > largestLineLength) {
            largestLineLength = length;
        }
    }

    List<String> outputs = new ArrayList<String>();

    for (String s : list.subList(0, list.size() - 1)) {

        int needSpace = largestLineLength - s.length();
        if (needSpace > 0) { //check if we need space in this line.
            String[] words = s.split(" "); //find words in the line
            int index = 0;

            StringBuilder[] stringBuilders = new StringBuilder[words.length];

            for (int i = 0; i < words.length - 1; i++) {
                stringBuilders[i] = new StringBuilder(words[i]);
                stringBuilders[i].append(" ");
            }

            stringBuilders[words.length - 1] = new StringBuilder(words[words.length - 1]);

            while (needSpace > 0) { //add spaces and decrease needSpace until it reaches zero.
                stringBuilders[index].append(" "); //add space to the end of every world in order
                index = index == words.length - 2 ? 0 : index + 1; //words-2 is because we didnt want any spaces after last word of line.
                needSpace--;
            }

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < words.length; i++) {
                stringBuilder.append(stringBuilders[i]);
            }


            s = stringBuilder.toString();
        }
        outputs.add(s);
    }
    outputs.add(list.get(list.size()-1));

    for(String s : outputs){
        System.out.println(s);
    }
}

Luke  Skywalker  has  returned
to his home planet of Tatooine
in   order   to  —  okay,  you
know   what,  we  don't  care.
We  were  thinking of not even
doing this one.

关于java - 通过添加空格使字符串大小相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30412238/

相关文章:

string - BATCH - 使用 string(<, >) 而不是重定向

java - 如何检查两个单词是否是字谜

java - 如何在 Groovy Selenium 中初始化 WebDriver

java - 有人知道 SWT 中的 PropertyEditor 或 PropertyGrid 之类的东西吗?

java - 哈希表替代方案以获得更好的性能

java - Android 如何使用 OkHttp 从回调中获取响应字符串?

java - 如何修剪 java stringbuilder?

java - 推荐的 REST API 日期格式

java - 在java中生成随机字符串

c# - 将由返回字符分隔的字符串转换为 List<string> 的最佳方法是什么?