Rearrange text and format it to initialize variables in C#

[initialize variables]

This example takes three columns of strings, rearranges them so the values in each column are together, and reformats the results to produce text that you can use to initialize variables.

The Shakespeare Insult Kit contains a table holding three columns of words. To generate a Shakespeare-style insult, you pick one word from each column and combine them. I wanted to write a program to do this, but the data isn’t in the right format to initialize variables. The web page lists the data in three columns of text. I need it in the form of code to initialize variables that are string arrays, one for each column.

The program uses the following code to include the insult data as a large string. (I just copied and pasted this into the program. Many values are omitted here to save space.)

// The data (from
private string all =
@"artless             base-court          apple-john
bawdy               bat-fowling         baggage
beslubbering        beef-witted         barnacle
bootless            beetle-headed       bladder
yeasty              weather-bitten      wagtail";

When you click the Switch button, the following code executes.

// Switch rows and columns.
private void btnSwitch_Click(object sender, EventArgs e)
    // Split the data into lines.
    string[] newline = { "\r\n" };
    string[] lines = all.Split(newline,

    // Split the lines into words.
    int num_rows = lines.Length;
    int num_cols = 3;
    string[,] words = new string[num_rows, num_cols];
    for (int row = 0; row < num_rows; row++)
        string[] space = { " " };
        string[] line_words = lines[row].Split(space,
        for (int col = 0; col < num_cols; col++)
            words[row, col] = line_words[col];

    // Generate the output.
    string result = "";
    for (int col = 0; col < num_cols; col++)
        result += "        private string[] Column" +
            col.ToString() + " =\r\n";
        result += "        {\r\n";
        for (int row = 0; row < num_rows; row++)
            result += "            \"" +
                words[row, col] + "\",\r\n";
        result += "        };\r\n\r\n";

    // Display the result.
    txtResult.Text = result;

First, the code uses the string class’s Split method to split the original big string into lines separated by the carriage return line feed combination \r\n.

Next, the program creates a two-dimensional array to hold the three words in each line of text. It loops over each row and save the row’s words in the array.

The program then generates C# code to initialize variables to hold the data. For column numbers 0 through 2, it creates code that looks like this:

        private string[] Column0 =

The code finishes by displaying the result.

My next post will use this code as data for a Shakespeare insult generator.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in strings, variables and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.