Function – Splitting a string

Splitting a String on a Character

There are a lot of functions out there that are very simple and easy to understand but are absolutely necessary to make fully functional applications. This week, we are going to begin with a simple string function: split. The split function (explode in PHP) allow the programmer to take a string and split that string into an array of strings on a character. For example, you could split a sentence into words by splitting on a space. As I said, this is a very simple function to use and understand.

Visual Basic

'Build the string we want to split
Dim stringToSplit As String = "this,is,a,string,that,needs,split"

'Array of strings to store the split values in
Dim stringArray As String()

'Split the sting based on a character value
stringArray = stringToSplit.Split(","C)

'Iterate through the results from the split
For Each splitString As String In stringArray
	'add each string to a listbox
	lstStrings.Items.Add(splitString)
Next

We take a string and split it on a comma and add it to a list box control named lstStrings.

C#

            //Build the string we want to split
            string stringToSplit = "this,is,a,string,that,needs,split";

            //Array of strings to store the split values in
            string[] stringArray;
            
            //Split the sting based on a character value
            stringArray = stringToSplit.Split(',');
            
            //Iterate through the results from the split
            foreach (string splitString in stringArray)
            {
                //add each string to a listbox
                lstStrings.Items.Add(splitString);
            }

We take a string and split it on a comma and add it to a list box control named lstStrings.

PHP

//Create a string to split
$stringToSplit = "this,is,a,string,that,needs,split";

//Just showing this value is an array (not neccessary with PHP)
$stringArray = array();

//Split the string on a character (explode in PHP is a split function)
$stringArray = explode($stringToSplit,',');

//Start table HTML
$html = "<table>";
//Iterate through the array
foreach($stringArray as $stringSingle)
{
    //Build the rows for the table
   $html .= "<tr>".$stringSingle."</tr>";
}

//End table HTML
$html .= "</table>";

//Output the table
echo $html;

We take a string and split it into an array. Then we iterate through the array and put the values into a table.

Java

        //Create a string to split
        String stringToSplit = "this,is,a,string,that,needs,split";

        //Create array for string to be split into and run split on it
        final String stringArray[] = stringToSplit.split(",");

        //Iterate through the array
        for(int r=0;r<stringArray.length; r++)
        {
            stringList.setModel(new javax.swing.AbstractListModel() {
                String[] strings = stringArray;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
        }

Split the string and then add the values to a jList control.

As you can see, each language has it’s unique way of using this function but I think we can all agree that this is simple to understand and can be very helpful in our applications. Nice thing about this function is that if the string doesn’t contain the character you want to split on, it will still work as you will just get one string since there is nothing to split. It won’t throw an error.

That concludes this week’s tutorial. Hopefully it was easy to follow and understand and we’ll see you next week. Thanks for viewing.