Monday, June 22, 2015

string to array, array to string via string builder.

Change comma separated string to array of string.
separate the string with comma into an array.
eg:

private void validateCompanyList()
{
    //txtCompany output = "Company1,    company2, company3,company4";
    string[] companyList = txtCompany.Text.Split(',').ToArray();
    if (companyList.Count() > 0)
    {
        foreach (string company in companyList)
        {
            //eg: 1st iteration => company = "Company1"
            //     2nd: company =  "    company2"
            //     3rd : company = " company3"
            //     4th : company = "company4"

            //if you take the company.trim(), then you will get
            //     1st: company.trim() = "Company1"
            //     2nd: company.trim() = "company2"
            //     3rd: company.trim() = "company3"
            //     4th: company.trim() = "company4"
        }
    }
         
}

you can then use stringBuilder to join back the company into a string.
for example:

using System.Text;

private void validateCompanyList()
{
    if (string.IsNullOrWhiteSpace(txtCompany.Text))
        return;

    string[] companyList = txtCompany.Text.Split(',').ToArray();
    StringBuilder sbErrorMsg = new StringBuilder();

    if (companyList.Count() > 0)
    {
        foreach (string company in companyList)
        {
            int found = CompanyObject.Where(t=> t.COMPANY_CODE == company.Trim().ToUpper() && t.REC_DELETED == Constant.RECORD_ACTIVE).Count();
 
            if (found == 0)
            {
                if (sbErrorMsg.Length != 0)
                    sbErrorMsg.Append(",");

                sbErrorMsg.Append(company.Trim());
            }                  
        }
    }
    if (sbErrorMsg.Length != 0)
    {
        ErrorMessage.errorMessageControl(string.Concat("Invalid company code(s) : " , sbErrorMsg));              
    }
         
}

Beside text.split(','); you can also take a look at text.split(new char[] { ',' }, StringSplitOptions), where you can choose StringSplitOptions.None or StringSplitOptions.RemoveEmptyEntries.
let's take a look at the second option :
string[] companySpliter = txtCompany.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

With this option, the return value doesn't not include array element that is empty string. This is a very good way if you want to exclude null string in array after split.

eg: txtCompany.Text = ",,,,,,";
companySpliter.length is 0 for this case.
while string[] companySpliter2 = txtCompany.Text.Split( ',' );
will have companySpliter2.length equal to 7, with all the child element having string.Empty.

However, StringSplitOptions.RemoveEmptyEntries will not exclude child element that contain only space(s).
eg: txtCompany.Text = " ,  ";
companySpliter.length is 2 for this case.
same with string[] companySpliter2 = txtCompany.Text.Split( ',' );
which will have companySpliter2.length equal to 2.

You will still need string.IsNullOrWhiteSpace(company) to check while looping the array if you wish to ignore the spaces.

No comments:

Post a Comment