{"id":661,"date":"2021-11-15T15:36:52","date_gmt":"2021-11-15T20:36:52","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=661"},"modified":"2024-02-06T12:10:32","modified_gmt":"2024-02-06T17:10:32","slug":"splitting-strings-in-java-2","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/splitting-strings-in-java-2\/","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-preformatted\">public class SplitString {\n\n   public static void main(String[] args) {\n       \/\/ splitting on a character\n       String str = \"splitstring\";\n       String[] 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(\"[$#@%&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(\"[0-9]+\");\n       for (String i : t) {\n           System.out.print(i + \" \");\n       }\n       System.out.println();\n   }\n}\n<\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">spli s ring \nspli ring \nsplit string \nsplit string \nsplit string on muliple speical symbols \nsplit symbols \nregular expression \n<\/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. public class SplitString { public static void main(String[] args) { \/\/ splitting on a character String str = &#8220;splitstring&#8221;; String[] t = str.split(&#8220;t&#8221;); for (String i : t) { [&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,76,65],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-programming","tag-string-manipulation"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/661","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=661"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/661\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}