{"id":136,"date":"2020-03-05T06:32:00","date_gmt":"2020-03-05T06:32:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=136"},"modified":"2020-12-02T06:35:17","modified_gmt":"2020-12-02T06:35:17","slug":"splitting-strings-in-java","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/splitting-strings-in-java\/","title":{"rendered":"Splitting strings in Java"},"content":{"rendered":"\n<p>Java&#8217;s split function packs a powerful punch. You can use it to split string on characters, symbols, substrings, a collection of symbols, or even regular expressions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SplitString {\n\n    public static void main(String&#91;] args) {\n\n        \/\/ splitting on a character\n        String str = \"splitstring\";\n        String&#91;] t = str.split(\"t\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n\n        \/\/ splitting on a substring\n        str = \"splitstring\";\n        t = str.split(\"tst\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n\n        \/\/ splitting a digit\n        str = \"split4string\";\n        t = str.split(\"4\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n\n        \/\/ splitting one special symbols\n        str = \"split$string\";\n        t = str.split(\"\\\\$\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n\n        \/\/ splitting multiple special symbols\n        str = \"split$string#on@muliple%speical&amp;symbols\";\n        t = str.split(\"&#91;$#@%&amp;]\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n\n        \/\/ splitting with regular expressions\n        str = \"split9symbols\";\n        t = str.split(\"\\\\d\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n\n        \/\/ splitting with regular expressions\n        str = \"regular9expression\";\n        t = str.split(\"&#91;0-9]+\");\n        for (String i : t) {\n            System.out.print(i + \" \");\n        }\n        System.out.println();\n    }\n}<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>spli s ring \nspli ring \nsplit string \nsplit string \nsplit string on muliple speical symbols \nsplit symbols \nregular expression <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>First part of the code splits the string on the character &#8220;t&#8221;<\/li><li>Second part splits on the substring &#8220;tst&#8221;<\/li><li>Third part splits on the number 4<\/li><li>Fourth part split on the dollar sign ($). Note the double backslash to escape the character. Similarly use \\t for tabs and \\n for newline characters.<\/li><li>Fifth part splits on $, #, @, %, and &amp; symbols. Note the use of square brackets [] to make this possible.<\/li><li>Sixth part splits on any digit. \\d is a regular expression abbreviation for &#8220;match any digit&#8221;. Note the extra backslash to escape \\d.<\/li><li>Last part also splits on any digit using a regular expression.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java&#8217;s split function packs a powerful punch. You can use it to split string on characters, symbols, substrings, a collection of symbols, or even regular expressions. output First part of the code splits the string on the character &#8220;t&#8221; Second part splits on the substring &#8220;tst&#8221; Third part splits on the number 4 Fourth part [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38,65],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-string-manipulation"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/136\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}