{"id":20505,"date":"2022-09-15T23:21:48","date_gmt":"2022-09-15T17:51:48","guid":{"rendered":"https:\/\/java2blog.com\/?p=20505"},"modified":"2022-09-15T23:48:36","modified_gmt":"2022-09-15T18:18:36","slug":"replace-comma-with-space-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/replace-comma-with-space-java\/","title":{"rendered":"How to Replace Comma with Space in Java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Replace_comma_with_space_in_java\">Replace comma with space in java<\/a><ul><li><a href=\"#1_Using_replace_method\">1. Using replace() method<\/a><\/li><li><a href=\"#2_Using_replaceAll_method\">2. Using replaceAll() method<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<p>Learn about how to replace comma with space in java.<\/p>\n<h2><span id=\"Replace_comma_with_space_in_java\">Replace comma with space in java<\/span><\/h2>\n<h3><span id=\"1_Using_replace_method\">1. Using replace() method<\/span><\/h3>\n<p>Use String&#8217;s <code>replace()<\/code> method to replace comma with space in java.<\/p>\n<p>Here is syntax of <code>replace()<\/code> method:<\/p>\n<pre code=\"java\" title=\"replace method syntax\">\npublic String replace(CharSequence target, CharSequence replacement)  \n<\/pre>\n<pre code=\"java\" title=\"Using replace method\" mark=\"8-9\">\npackage org.arpit.java2blog;\n\npublic class ReplaceCommaWithSpaceMain {\n\n    public static void main(String[] args) {\n        String str = \"1,2,3,4\";\n        str = str.replace(\",\",\" \");\n        System.out.println(str);\n    }\n}\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\n1 2 3 4\n<\/div>\n<p>As you can see, <code>replace()<\/code> method replaced each comma with space in the String.<\/p>\n<h3><span id=\"2_Using_replaceAll_method\">2. Using <code>replaceAll()<\/code> method<\/span><\/h3>\n<p>Use <code>replaceAll()<\/code> method to replace comma with space in java. It is identical to <code>replace()<\/code> method, but it takes regex as argument. You can go through <a href=\"https:\/\/java2blog.com\/difference-between-replace-replaceall-java\/\" title=\"difference between replace and replaceAll\">difference between replace and replaceAll<\/a> over here.<\/p>\n<p>Here is syntax of replaceAll method:<\/p>\n<pre code=\"java\" title=\"replaceAll method syntax\">\npublic String replaceAll(String regex, String replacement)  \n<\/pre>\n<pre code=\"java\" title=\"Using replaceAll method\" mark=\"8\">\npackage org.arpit.java2blog;\n\npublic class ReplaceCommaWithSpaceMain {\n    public static void main(String[] args) {\n        String str = \"1,2,3,4\";\n        str = str.replaceAll(\",\",\" \");\n        System.out.println(str);\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\n1 2 3 4\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/remove-substring-from-string-in-java\/\" target=\"_blank\" rel=\"noopener\">Remove substring from string in java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/remove-substring-from-string-in-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/remove-comma-from-string-java\/\" target=\"_blank\" rel=\"noopener\">Remove comma from String in Java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/remove-comma-from-string-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<p>That&#8217;s all about how to replace comma with space in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String&#8217;s replace() method to replace comma with space in java. Here is syntax of replace() method: public String replace(CharSequence target, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[5,27],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20505"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20505"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20505\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}