{"id":4617,"date":"2019-10-19T13:31:09","date_gmt":"2019-10-19T04:31:09","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=4617"},"modified":"2019-10-19T13:43:05","modified_gmt":"2019-10-19T04:43:05","slug":"java-string","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/java-string","title":{"rendered":"Summary of Basic String Manipulation in Java"},"content":{"rendered":"<h2>Split string - <strong>split<\/strong><\/h2>\n<p>Split a string with a specific string (delimiter).<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void splitSample() {\r\n    String line = \"apple,mandarin orange,banana\";\r\n    String[] fruits = line.split(\",\");\r\n    for (String fruit : fruits) {\r\n        System.out.println(fruit);\r\n    }\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\napple\r\nmandarin orange\r\nrape (Brassica rapa var. amplexicaulu)\r\n<\/pre>\n<h2>Basic String Concatenation<\/h2>\n<p>Strings can be combined with the \"+\" operator.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void plusSample() {\r\n    String s1 = \"hello \";\r\n    String s2 = \"world!\";\r\n    System.out.println(s1 + s2);\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nhello world!\r\n<\/pre>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2>Combine strings with delimiters -. <strong>join<\/strong><\/h2>\n<p>Combines strings with a specific string (delimiter) (available in java8 or later)<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void joinSample() {\r\n    String line = String.join(\",\"\", \"apple\", \"mandarin\", \"banana\");\r\n    System.out.println(line);\r\n}\r\n<\/pre>\n<p>join can be joined with string arrays in the same way.<\/p>\n<pre class=\"sample_src\">\r\npublic static void joinArraySample() {\r\n    String[] fruits = {\"apple\", \"mandarin\", \"banana\"};\r\n    String line = String.join(\",\"\", fruits);\r\n    System.out.println(line); \/\/result is the same\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nApples, oranges, bananas\r\n<\/pre>\n<h2>Cut out a string from a specified range of characters. <strong>substring<\/strong><\/h2>\n<p>Crop a portion of a string by specifying the start and end points of the cutout.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void substringSample() {\r\n    String str = \"hello world!\";\r\n    System.out.println(str.substring(0, 5));\r\n    System.out.println(str.substring(2, 9));\r\n    System.out.println(str.substring(6)); \/\/Only the starting point can be specified\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nhello\r\nllo wor\r\nworld!\r\n<\/pre>\n<h2>\u524d\u5f8c\u306e\u7a7a\u767d\u3092\u524a\u9664\u3059\u308b \u2013 <strong>trim<\/strong><\/h2>\n<p>Remove half-width spaces, line breaks, and tabs before and after a string.<br \/>\nBlanks and double-byte spaces in the middle of the string are not removed.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void trimSample() {\r\n    String str = \" hello world!\";\r\n    System.out.println(str.trim());\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nhello world!\r\n<\/pre>\n<h2>\u6587\u5b57\u5217\u306e\u4e00\u90e8\u3092\u5225\u306e\u6587\u5b57\u5217\u3067\u7f6e\u304d\u63db\u3048\u308b \u2013 <strong>replace<\/strong><\/h2>\n<p>Replaces part of a string with the specified string. replace replaces all matching strings.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void replaceSample() {\r\n    String str = \"hello world!\";\r\n    System.out.println(str.replace(\"l\", \"x\"));\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nhe x x o wor xd!\r\n<\/pre>\n<p>replaceFirst replaces only the first matching string.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void replaceFirstSample() {\r\n    String str = \"hello world!\";\r\n    System.out.println(str.replaceFirst(\"l\", \"x\"));\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nhe x lo world!\r\n<\/pre>\n<h2>Returns the number of characters in a string - - Returns the number of characters in a string <strong>length<\/strong><\/h2>\n<p>Returns the number of characters in a string; String.length() simply returns the number of characters.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void lengthSample() {\r\n    String str = \"10-11, \u25b3\u25b3-cho, \u25a1\u25a1 city, 00 prefecture\";\r\n    System.out.println(str.length());\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\n14\r\n<\/pre>\n<p>If you want to know the number of bytes, use String.getBytes().length; since the standard character encoding from Java8 is UTF8, each double-byte character is 3 bytes.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\npublic static void byteLengthSample() {\r\n    String str = \"10-11, \u25b3\u25b3-cho, \u25a1\u25a1 city, 00 prefecture\";\r\n    System.out.println(str.getBytes().length);\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\n32\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Split a string \u2013 split Splits a string at a specific string (delimiter). Sample code [\u2026]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4617"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=4617"}],"version-history":[{"count":8,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4617\/revisions"}],"predecessor-version":[{"id":4628,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4617\/revisions\/4628"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=4617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=4617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=4617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}