Java 格式化字符串以适应输出控制台中的长方体形状

Java 格式化字符串以适应输出控制台中的长方体形状,java,string,Java,String,所以我试图将一个名为description的字符串放入一个框中。例如: |This is the description | |of this card. It is very | |interesting | | | | | | | | | 我的代码就是这样做的

所以我试图将一个名为description的字符串放入一个框中。例如:

|This is the description   |
|of this card. It is very  |
|interesting               |
|                          |
|                          |
|                          |
|                          |
我的代码就是这样做的。不过,恐怕我的效率太低了

那么我的问题是,我的方法和代码有效吗?或者有没有其他更方便的方法不使用库来处理和编码这个问题?我不是要求你为我重新编写代码。我所需要的只是有人向我口头解释我可以改进什么,我如何错误地处理问题,我可以使用什么资源来帮助我等等

我的做法:

  • 将字符串中的所有单词放入数组
  • 创建一个数组以包含需要打印的所有行
  • 如果合适,请将单词插入行中,如果不合适,请移到下一行
  • 注意:如果描述太长,那么它会被缩短,我不介意

    这就是我的代码(我意识到它非常庞大,这就是为什么我觉得有更好的方法来做这件事):

    公共字符串[]描述格式(字符串描述)
    {
    int wordCount=1;//int用于跟踪字符串中的单词数量
    for(int i=0;i
    使用regex和
    printf()
    ,可以这样做:

    static void打印列(字符串输入,int-width){
    字符串格式=“|%-”+width+“s |%n”;
    Matcher m=Pattern.compile(“\\s*+(.{1,“+width+”})(?:\\s++$)”)。Matcher(输入);
    while(m.find())
    System.out.printf(格式,m.group(1));
    }
    
    测试1

    printColumn(“这是对这张卡的描述,非常有趣”,26);
    
    |这是对|
    |这张卡。这是非常重要的|
    |有趣的|
    
    测试2

    printColumn(“这是此卡的说明。\n”+
    “这很有趣”,26);
    
    |这是对|
    |这张卡|
    |这很有趣|
    
    我投票决定结束这个问题,因为它属于。好问题,错误的站点。description.split(“”)是否有助于简化代码?
    public String[] descriptionFormat(String description)
        {
            int wordCount = 1; //Int for keeping track words amount in String
            for(int i = 0; i < description.length(); i++) //Count the amount of words in the String
            {
                if(description.charAt(i) == ' ')
                {
                    wordCount++;
                }
            }
            
            String words[] = new String[wordCount]; //Array of words
            String temp = description;
            for(int i = 0; i < wordCount; i++) //loop for filling array with words
            {
                String word = "";
                if(temp.indexOf(' ') != -1)
                {
                    word = temp.substring(0,temp.indexOf(' '));
                    temp = temp.substring(temp.indexOf(' ') + 1);
                }
                else
                {
                    word = temp.substring(0,temp.length());
                }
                words[i] = word;
            }
    
            String descriptionArray[] = new String[7]; //Array of description rows
            for(int i = 0; i < descriptionArray.length; i++) //Fill array with non null values
            {
                descriptionArray[i] = "";
            }
            
            int word = 0; //int to keep track which word we trying to insert
            for(int i = 0; i < descriptionArray.length; i++)
            {
                int index = 0; //index for how much characters we already have in a row
                
                while(word < words.length && words[word].length() < (rowWidth - 3) - index ) //while the word can fit into the row
                {
                    index += words[word].length() + 1;
                    descriptionArray[i] += words[word] + " ";
                    word++;
                }
                
            }
            
            for(int i = 0; i < descriptionArray.length; i++)
            {
                descriptionArray[i] = "|" + descriptionArray[i]; //add left border
                while(descriptionArray[i].length() < rowWidth - 2) //insert appropriate spaces
                {
                    descriptionArray[i] += " "; 
                }
                descriptionArray[i] = descriptionArray[i] + "|"; //add right border
            }
            /*
            for(int i = 0; i < descriptionArray.length; i++) //print rows for test
            {
                System.out.println(descriptionArray[i]);
            }
            */
            return descriptionArray;
        }