{"id":138,"date":"2020-03-09T06:42:00","date_gmt":"2020-03-09T06:42:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=138"},"modified":"2024-02-02T16:19:26","modified_gmt":"2024-02-02T21:19:26","slug":"replacing-and-removing-subtrings-in-java","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/replacing-and-removing-subtrings-in-java\/","title":{"rendered":"Replacing and removing subtrings in Java"},"content":{"rendered":"\n<p>Java&#8217;s replaceString function is very handy for string and substring modifications.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ReplaceString {\n\n    public static void main(String&#91;] args) {\n\n        \/\/ replacing a character in string\n        String str = \"replacestring\";\n        System.out.println(str.replace(\"a\",\"e\"));\n\n        \/\/ removing a character from string\n        System.out.println(str.replace(\"a\", \"\"));\n\n        \/\/ replacing a substring in a string\n        System.out.println(str.replace(\"replace\", \"remove\"));\n\n        \/\/ removing a substring in a string\n        System.out.println(str.replace(\"replace\", \"\"));\n\n        \/\/ replacing a symbol from a string\n        str = \"$40.36\";\n        str = str.replace(\"$\", \"CAD \");\n        System.out.println(str);\n    }\n}<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>replecestring\nreplcestring\nremovestring\nstring\nCAD 40.36<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>First part of the all occurrences of a with e<\/li><li>Second part remove all occurrences of the character a<\/li><li>Third part replaces the substring replace with the substring remove<\/li><li>Fourth part removes ths substring replace<\/li><li>Last part shows that special symbols can also be replaced. They don&#8217;t need to be escaped with backslash<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java&#8217;s replaceString function is very handy for string and substring modifications. output First part of the all occurrences of a with e Second part remove all occurrences of the character a Third part replaces the substring replace with the substring remove Fourth part removes ths substring replace Last part shows that special symbols can also [&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-138","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\/138","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=138"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/138\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}