简体   繁体   English

文本 alignment 和 C#

[英]Text alignment with C#

I have a string of text with lines:我有一串带线条的文本:

string s = "word longerword longestword evenlongerword aditionalword\n
longerword word evenlongerword longestword"

I want to align them like this (one word under another with 1 whitespace in between words):我想像这样对齐它们(一个单词在另一个单词之间,单词之间有 1 个空格):

s = "word       longerowrd longestword    evelongerword aditionalword
     longerword word       evenlongerword longestword"

Further explanation: The text is as one continuous string with '\n'(newline) character breaking the text into lines.进一步说明:文本是一个连续的字符串,带有'\n'(换行符)字符将文本分成几行。 There are no words longer than 80 characters in this example (thus word splitting or '-' won't be needed).此示例中的单词长度不超过 80 个字符(因此不需要分词或“-”)。 I want all words in the column aligned so that no word in next column starts in a whitespace position of the word before if that makes sense.我希望列中的所有单词对齐,以便下一列中的任何单词都没有以之前单词的空格 position 开头,如果这有意义的话。

Optionally, there should be less than 80 characters in each row. (可选)每行应少于 80 个字符。 How should I approach this?我应该如何处理这个?

It's not pretty, but this code will definitely output what you want:它不漂亮,但这段代码肯定会 output 你想要什么:

string[] lines = "word longerword longestword evenlongerword aditionalword\nlongerword word evenlongerword longestword".Split('\n');
string result;
Dictionary<int, int> wordSize = new Dictionary<int, int>();

// Build word sizes first
foreach (string line in lines)
{
    string[] words = line.Split(' ');

    for (int i = 0; i < words.Length; i++)
    {
        if (!wordSize.ContainsKey(i))
            wordSize.Add(i, 0);
        if (wordSize[i] < words[i].Length)
            wordSize[i] = words[i].Length;
    }
}

// Output results
result = string.Empty;
foreach (string line in lines)
{
    string[] words = line.Split(' ');

    for (int i = 0; i < words.Length; i++)
        result += words[i].PadRight(wordSize[i] + 1, ' ');
    result = result.TrimEnd();
    result += "\n";
}

Console.WriteLine(result);

Output: Output:

word       longerword longestword    evenlongerword aditionalword
longerword word       evenlongerword longestword

How it works这个怎么运作

First, the program determines how long each word is.首先,程序确定每个单词的长度。 It does this by going through each line, and then each word in that line.它通过遍历每一行,然后是该行中的每个单词来做到这一点。

The resulting word length is stored in the dictionary wordSize , with key 0 indicating the first word, key 1 indicating the second word, and so on.生成的单词长度存储在字典wordSize中,键0表示第一个单词,键1表示第二个单词,依此类推。 Of course, the value is only updated if it exceeds the length of the word from the previous line.当然,只有当它超过上一行的单词长度时,才会更新该值。

After this, it goes back through each line, and each word, and then adds the padded word to the output, plus 1 character for the extra space.在此之后,它会返回每一行和每个单词,然后将填充的单词添加到 output,加上 1 个字符作为额外的空格。

string s = "word longerword longestword evenlongerword aditionalword word longerword longestword evenlongerword aditionalword word longerword longestword evenlongerword aditionalword";
StringBuilder alignedString = new StringBuilder();
string[] words = s.Split(' ');
foreach (var word in words)
{
    if (alignedString.Length + word.Length > 80)
        alignedString.Append(Environment.NewLine);
    alignedString.Append(word);
    alignedString.Append(" \t");
}
return alignedString.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM