'Looking for an easy way to align text
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:
x="foo" data="123" y=x
x="b 4" data="12 " y=x
x="baaar4" data="123aaa5" y=x
x="baaaa,4" data="123dddd5" y=x
becomes this
x="foo" data="123" y=x
x="b 4" data="123 " y=x
x="baaar4" data="123aaa5" y=x
x="baaaa,4" data="123dddd5" y=x
This is a text issue. The input comes from notepad. The output is going to notepad. This is not a formatting issue.
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
ElasticTabstops is actually exactly doing the job I was looking for when you are working with tabs. Aligning without adding characters (spaces or tabs).
Solution 4:[4]
I have created a Python Script that can be used with Python Script Plugin in Notepad++: nppPyAlignColumn
Solution 5:[5]
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).
Solution 6:[6]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Emiliano A. Carlevaro |
| Solution 3 | Dorian Grv |
| Solution 4 | Noel Evans |
| Solution 5 | phuclv |
| Solution 6 | trinib |

