String Array (Text Lines ) To Single Line

Array To Line is a small tool that is meant to convert an array of lines to single line of text with proper formatting and concatenation string.

Usage : assume you are executing a SQL query against your database and you are using then IN operator that allows you to specify multiple values in a WHERE clause , but the values here is from an EXCEL Sheet ( column ) with about 100 or more value !! of course you will hate your self while your are formatting the SQL statement and you will hate your self more if you made a mistake .

but if have only to copy all values to a Text Box in your tool and click a Button on the screen and get the result values with the proper quotes and separator ready for execute I think that you will be happy :)

The Code :
create as GUI like the bellow screen shoot


Name the TextBoxes like :

Input TextBox --> txtArrayInput
Separator TextBox --> txtSeparator
Quotes TextBox --> txtQuotes
Result TextBox --> txtArrayResult


And the CheckBoxes like :

Ignore Empty Line --> chkIgnoreEmptyLine
Trim ( to apply on each line ) --> chkTrim


put the following code in the ( Combine ) Button Click Event

 var myStr = new StringBuilder();
 int mCount = 0;
 for (int ii = 0; ii < txtArrayInput.Lines.Length; ii++)
 {
     if (chkIgnoreEmptyLine.Checked)
     {
         if (txtArrayInput.Lines[ii].Trim().Length > 0)
         {

            myStr.Append(txtQuotes.Text + ((chkTrim.Checked) ? txtArrayInput.Lines[ii].Trim() : txtArrayInput.Lines[ii]) + txtQuotes.Text + txtSeparator.Text);
            mCount++;
   
         }
     }
     else
     {
        myStr.Append(txtQuotes.Text + ((chkTrim.Checked) ? txtArrayInput.Lines[ii].Trim() : txtArrayInput.Lines[ii]) +     txtQuotes.Text + txtSeparator.Text);
        mCount++;
        
     }
 }

 txtArrayResult.Text = myStr.ToString(); 
 lblItemsCount.Text = mCount.ToString() + " Items Combined ";


No comments:

Post a Comment