{"id":2620,"date":"2010-10-18T23:59:50","date_gmt":"2010-10-19T03:59:50","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=2620"},"modified":"2010-10-28T08:43:55","modified_gmt":"2010-10-28T12:43:55","slug":"function-splitting-a-string","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string","title":{"rendered":"Function – Splitting a string"},"content":{"rendered":"

Splitting a String on a Character<\/p>\n

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.<\/p>\n

Visual Basic<\/strong><\/p>\n

\r\n'Build the string we want to split\r\nDim stringToSplit As String = "this,is,a,string,that,needs,split"\r\n\r\n'Array of strings to store the split values in\r\nDim stringArray As String()\r\n\r\n'Split the sting based on a character value\r\nstringArray = stringToSplit.Split(","C)\r\n\r\n'Iterate through the results from the split\r\nFor Each splitString As String In stringArray\r\n\t'add each string to a listbox\r\n\tlstStrings.Items.Add(splitString)\r\nNext\r\n<\/pre>\n

We take a string and split it on a comma and add it to a list box control named lstStrings.<\/p>\n

C#<\/strong><\/p>\n

\r\n            \/\/Build the string we want to split\r\n            string stringToSplit = "this,is,a,string,that,needs,split";\r\n\r\n            \/\/Array of strings to store the split values in\r\n            string[] stringArray;\r\n            \r\n            \/\/Split the sting based on a character value\r\n            stringArray = stringToSplit.Split(',');\r\n            \r\n            \/\/Iterate through the results from the split\r\n            foreach (string splitString in stringArray)\r\n            {\r\n                \/\/add each string to a listbox\r\n                lstStrings.Items.Add(splitString);\r\n            }\r\n<\/pre>\n

We take a string and split it on a comma and add it to a list box control named lstStrings.<\/p>\n

PHP<\/strong><\/p>\n

\r\n\/\/Create a string to split\r\n$stringToSplit = "this,is,a,string,that,needs,split";\r\n\r\n\/\/Just showing this value is an array (not neccessary with PHP)\r\n$stringArray = array();\r\n\r\n\/\/Split the string on a character (explode in PHP is a split function)\r\n$stringArray = explode($stringToSplit,',');\r\n\r\n\/\/Start table HTML\r\n$html = "<table>";\r\n\/\/Iterate through the array\r\nforeach($stringArray as $stringSingle)\r\n{\r\n    \/\/Build the rows for the table\r\n   $html .= "<tr>".$stringSingle."<\/tr>";\r\n}\r\n\r\n\/\/End table HTML\r\n$html .= "<\/table>";\r\n\r\n\/\/Output the table\r\necho $html;\r\n<\/pre>\n

We take a string and split it into an array. Then we iterate through the array and put the values into a table.<\/p>\n

Java<\/strong><\/p>\n

\r\n        \/\/Create a string to split\r\n        String stringToSplit = "this,is,a,string,that,needs,split";\r\n\r\n        \/\/Create array for string to be split into and run split on it\r\n        final String stringArray[] = stringToSplit.split(",");\r\n\r\n        \/\/Iterate through the array\r\n        for(int r=0;r<stringArray.length; r++)\r\n        {\r\n            stringList.setModel(new javax.swing.AbstractListModel() {\r\n                String[] strings = stringArray;\r\n                public int getSize() { return strings.length; }\r\n                public Object getElementAt(int i) { return strings[i]; }\r\n            });\r\n        }\r\n<\/pre>\n

Split the string and then add the values to a jList control. <\/p>\n

As you can see, each language has it\u2019s 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\u2019t 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\u2019t throw an error. <\/p>\n

That concludes this week\u2019s tutorial. Hopefully it was easy to follow and understand and we\u2019ll see you next week. Thanks for viewing. <\/p>\n","protected":false},"excerpt":{"rendered":"

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 … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[121,25],"tags":[],"yoast_head":"\nFunction - Splitting a string<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Maguire\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string#webpage\",\"url\":\"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string\",\"name\":\"Function - Splitting a string\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"datePublished\":\"2010-10-19T03:59:50+00:00\",\"dateModified\":\"2010-10-28T12:43:55+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"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\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Function – Splitting a string\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\",\"name\":\"Mike Maguire\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"caption\":\"Mike Maguire\"},\"sameAs\":[\"http:\/\/mikemaguire.me\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Function - Splitting a string","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string","twitter_misc":{"Written by":"Mike Maguire","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string#webpage","url":"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string","name":"Function - Splitting a string","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"datePublished":"2010-10-19T03:59:50+00:00","dateModified":"2010-10-28T12:43:55+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"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","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/function-splitting-a-string#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Function – Splitting a string"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b","name":"Mike Maguire","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","caption":"Mike Maguire"},"sameAs":["http:\/\/mikemaguire.me"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/2620"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=2620"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/2620\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=2620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=2620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=2620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}