简体   繁体   中英

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):

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. There are no words longer than 80 characters in this example (thus word splitting or '-' won't be needed). 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.

Optionally, there should be less than 80 characters in each row. How should I approach this?

It's not pretty, but this code will definitely output what you want:

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:

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. 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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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