Looking for an easy way to align text

前端 未结 6 2042
悲哀的现实
悲哀的现实 2021-01-31 07:17

I have a common problem that I\'m looking for a solution to. I have lines of similar text I\'d like to somehow automatically insert text to align them vertically such that:

相关标签:
6条回答
  • 2021-01-31 08:02

    You did not list a programming language so here is some C# that performs the requested operation:

    int[] maxLengths = new int[100];
    string[][] splitLines = new string[input.Length][];
    for (int i = 0; i < input.Length; i++)
    {
        splitLines[i] = input[i].Split(' ');
        for (int j = 0; j < splitLines[i].Length; j++)
        {
            maxLengths[j] = Math.Max(maxLengths[j], splitLines[i][j].Length);
        }
    }
    for (int i = 0; i < splitLines.Length; i++)
    {
        for (int j = 0; j < splitLines[i].Length; j++)
        {
            Console.Write(string.Format("0,-" + (maxLengths[j] + 1) + ":G}", splitLines[i][j]));
        }
        Console.WriteLine();
    }
    

    Note that 100 has to be greater than or equal to the number of segments per line. You can cause that number to not be fixed with a little work if you like.

    Basically this algorithm splits each line based on spaces, then for each part calculates the maximum into a common array. Finally it loops through all these segmented parts and prints them out left-justified using spaces to the largest size (plus 1 to get the space between items).

    0 讨论(0)
  • 2021-01-31 08:06

    ElasticTabstops is actually exactly doing the job I was looking for when you are working with tabs. Aligning without adding characters (spaces or tabs).

    0 讨论(0)
  • 2021-01-31 08:10

    If you want to align by specific characters use Notepad++ extension TextFX.

    Look at: TextFX > TextFX Edit > Line up multiple lines by (,)

    There are also: Line up multiple lines by (=), and Line up multiple lines by (Clipboard Character)

    Here is a link for helpful tips for programming.

    http://techbrij.com/518/10-notepad-tips-tricks-fast-development

    0 讨论(0)
  • 2021-01-31 08:14

    Notepad++ has a plugin that does this for you. It's called "Code alignment". It allows you to align text vertically based on characters of your choosing. You can install it via the Plugin Manager for notepad.

    Below is a simple usage scenario. You start off with the following code, then you align by "equals" which is a built-in shortcut/command for the plugin.

    Initial Code Sample, without alignment

    After that, you end up with the following below:

    Code after equals alignment

    You can also take it a step further and use the "align by..." command which allows you to specify any arbitrary alignment string. In the example below, I chose the "#" character in order to align my comments next to one another. This could just as easily have been a longer string, and not one of length 1.

    End result below:

    Final result after arbitrary code alignment string

    0 讨论(0)
  • 2021-01-31 08:19

    I have created a Python Script that can be used with Python Script Plugin in Notepad++: nppPyAlignColumn

    0 讨论(0)
  • 2021-01-31 08:20

    Notepad++ TextFX is not longer supported and maintained. The Code alignment plugin only aligns the first occurence of the column aligning character.

    I solved the problem using a free online service: https://onlinetexttools.com/convert-text-to-nice-columns

    0 讨论(0)
提交回复
热议问题