Stringy things and splits

If you think about it a word is just an array of characters.
In c# this can quite useful.
To note for beginners, this arry does not need to be strings, it can be bools, integers, etc.
The common syntax in c# is:
string[] myString = {'c','a','t'};
or groups of strings:
string[] myWords = {"cat","dog","elephant"};
*Note the use of a single quote for characters and double quote for a string.

You can do the same with a boolean
bool[] myBool = {true,false,true,true};

Each of these items can be called by grabbing their index.
So if I wanted the a in the character string I would simply call myString[1]
Keep in mind that all of these are zero indexed, means the first element is index 0 and the second 1, 2, 3.

There are multiple ways to set these up.
One is to define the amount of elements.
string[] myStrings = new string[4]{};
This will create an indexed string[] with 4 elements. 0,1,2,3 (remember zero indexing)

This is also where Split comes in handy and you will use it often.

  • If you split on a word, then the .length of the split will be the length of the world
  • The last index of a split is always .length -1.
  • i.e. c[0] a[1] t[2] (length = 3 so (.length -1) = 2)

    If I had a comma delimited string (csv) file, I can split the file on carriage return(\n), then split each row on the comma(,).
    To loop through this I would do
    string[] rows = text.Split('\n'); //note that you are splitting on a character by putting in in single quotes.. in this case carriage return..
    Then do a loop to get each delimited row
    for (int i1=0; i < rows.length;i++)
    string[] rowsplit = rows[i1].Split(',');
    // if I wanted to put this into a table knowing that there are 3 columns I would simply do either
    string rowTD = ""+rowsplit[0] + "" + rowsplit[1] + "" + rowsplit[2] + "\n";
    or using string.Format would do:
    string rowTD=String.Format("{0}{1}{2}{0}",rows[i1][i2]);
    else if (i2==rows[i1].Length-1)
    row += string.Format("{0}\n",rows[i1][i2]);
    row += string.Format("{0}",rows[i1][i2]);
    }//end i2
    }//end i1

    Another cool part is you can combine a split and the index to get the value at that index.
    So if you have a string say
    string totalString ="total=100";
    You can do something like
    string cost = totalString.Split('=')[1];
    Response.Write("Your total cost is $" + cost + ");
    or save a few chars and do
    Response.Write("Your total cost is $"+totalString.Split('=')[1]);

    This one liner method works well with files.
    Take the string fullPath=c:\blah\blah\blah\myfile.text.
    If we do a split on the backslash we get the following elements:
    [c:] [blah] [blah] [blah] [file.text] ... so length is 5 total elements.
    0 1 2 3 4 are the indexes.
    To grab the last element which you know will be the file name:
    string fileName=fullPath.Split('\\')[fullPath.Split('\\').length-1];
    if you want to get the file association (.txt) you split the last element on the period:
    string fileAssociation = fileName.Split('.')[fileName.Split('.')Length-1];
    So fullPath.Spit('\\').Length = 5.. so the last split index would be 4 (or 5-1)

    Now you will either want to insure that the file is what you are expecting by either wrapping in a try/catch block
    or even better - insure that the index you are calling has the correct amount of expected indexes, (just in case the file does not have a file
    something like:
    if (fileName.Split('.')Length > 1)
    fileAssociation = fileName.Split('.')[fileName.Split('.')Length-1];
    to avoid errors... etc.

    One of the things I liked about string[]s is when doing configurations.

    If you use the index of 2 you will get Kentucky,2,Louisville
    This way as long as your indexes are correct, you will always get the right value from all of the string arrays.
    This comes in handy with something like AD connection strings
    You can populate this into say a dropdown llist and know that every time you click on the list index, it will select the items required for that index.
    i.e. clicking DC1 in the list (item[0]) will also select DOMAIN1,DC1Login,DC1PW.. get the picture? As long as your list has not changed it will carry across the indexes. If it may change, then I create an index checker method, where you pass in the list item, say DOMAIN2 and the method will loop through the list and return the index it finds that associates with the request.

    As long as you keep your indexing correct you can build a connection string simply by using the indexes of each of the required components of the connection string.
    Keeping in mind of course if you are using commas that the values do not contain a comma.
    That is why I prefer to use the | pipe symbol since distinguished names contain commas... i.e DC=blah,DC=247Coding,DC=com. This would break the indexing.
    Wrapping delimiters
    There is a way around commas in a comma delimted string, but it does not always work you will have to test it in your situation.
    If you know or are not sure if there will commas in your value, you can wrap each item with a quote. "item1","item2","item3". Programs like excel will accept this format and usually your delimiting will be happy.

    Replacement Splitting
    There are times where I may need to inject a special set of chars, the convert those chars into a single char for a split after the fact.
    Where this comes in handy is if say a web.config will not allow you to put an ampersand in, but you need to split on one. You can put in a special string in the web.config and then split it out by replacing the chars before you use the string.

    string configString = ConfigurationManager.AppSettings["cs"].ToString();
    string inject = "%AMPERSAND%";
    string[] targetString = configString.Replace(inject,"&").Split('&');
    This way you sneakily avoid the limitations of the configuration but happily appease your own requirements.

    More to come, this can and probably will be a lengthy topic.....