{"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\/vi\/blog\/java\/chuoi-java","title":{"rendered":"T\u1ed5ng h\u1ee3p c\u00e1c thao t\u00e1c chu\u1ed7i c\u01a1 b\u1ea3n trong Java"},"content":{"rendered":"<h2>T\u00e1ch m\u1ed9t chu\u1ed7i \u2013 <strong>t\u00e1ch ra<\/strong><\/h2>\n<p>T\u00e1ch m\u1ed9t chu\u1ed7i t\u1ea1i m\u1ed9t chu\u1ed7i c\u1ee5 th\u1ec3 (d\u1ea5u ph\u00e2n c\u00e1ch).<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void SplitSample() { Chu\u1ed7i line = &quot;t\u00e1o, cam, chu\u1ed1i&quot;; String[] Fruits = line.split(&quot;,&quot;); for (String Fruit : Fruits) { System.out.println(fruit); } }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nt\u00e1o cam chu\u1ed1i\r\n<\/pre>\n<h2>N\u1ed1i chu\u1ed7i c\u01a1 b\u1ea3n<\/h2>\n<p>B\u1ea1n c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p c\u00e1c chu\u1ed7i b\u1eb1ng to\u00e1n t\u1eed &quot;+&quot;.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void plusSample() { Chu\u1ed7i s1 = &quot;xin ch\u00e0o&quot;; Chu\u1ed7i s2 = &quot;th\u1ebf gi\u1edbi!&quot;; System.out.println(s1 + s2); }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nCh\u00e0o th\u1ebf gi\u1edbi!\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>N\u1ed1i chu\u1ed7i b\u1eb1ng d\u1ea5u ph\u00e2n c\u00e1ch \u2013 <strong>tham gia<\/strong><\/h2>\n<p>N\u1ed1i c\u00e1c chu\u1ed7i b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng m\u1ed9t chu\u1ed7i c\u1ee5 th\u1ec3 (d\u1ea5u ph\u00e2n c\u00e1ch) (c\u00f3 s\u1eb5n t\u1eeb Java 8)<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void joinSample() { String line = String.join(&quot;,&quot;, &quot;apple&quot;, &quot;orange&quot;, &quot;banana&quot;); System.out.println(line); }\r\n<\/pre>\n<p>join c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 n\u1ed1i c\u00e1c m\u1ea3ng chu\u1ed7i theo c\u00e1ch t\u01b0\u01a1ng t\u1ef1.<\/p>\n<pre class=\"sample_src\">\r\npublic static void joinArraySample() { String[] Fruits = {&quot;apple&quot;, &quot;qu\u00fdt&quot;, &quot;banana&quot;}; String line = String.join(&quot;,, Fruits); System.out.println(line); \/ \/K\u1ebft qu\u1ea3 gi\u1ed1ng nhau}\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nt\u00e1o, cam, chu\u1ed1i\r\n<\/pre>\n<h2>Tr\u00edch xu\u1ea5t m\u1ed9t ph\u1ea1m vi chu\u1ed7i \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh \u2013 <strong>chu\u1ed7i con<\/strong><\/h2>\n<p>Tr\u00edch xu\u1ea5t m\u1ed9t ph\u1ea7n c\u1ee7a chu\u1ed7i b\u1eb1ng c\u00e1ch ch\u1ec9 \u0111\u1ecbnh \u0111i\u1ec3m b\u1eaft \u0111\u1ea7u v\u00e0 \u0111i\u1ec3m k\u1ebft th\u00fac.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void substringSample() { String str = &quot;xin ch\u00e0o th\u1ebf gi\u1edbi!&quot;; System.out.println(str.substring(0, 5)); System.out.println(str.substring(2, 9)); System. out.println(str.substring(6)); \/\/B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 ch\u1ec9 \u0111\u1ecbnh \u0111i\u1ec3m b\u1eaft \u0111\u1ea7u}\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nxin ch\u00e0o th\u1ebf gi\u1edbi llo wor!\r\n<\/pre>\n<h2>X\u00f3a kho\u1ea3ng tr\u1eafng \u1edf \u0111\u1ea7u v\u00e0 cu\u1ed1i \u2013 <strong>c\u1eaft t\u1ec9a<\/strong><\/h2>\n<p>X\u00f3a kho\u1ea3ng tr\u1eafng n\u1eeda chi\u1ec1u r\u1ed9ng, ng\u1eaft d\u00f2ng v\u00e0 tab tr\u01b0\u1edbc v\u00e0 sau chu\u1ed7i.<br \/>\nC\u00e1c kho\u1ea3ng tr\u1ed1ng v\u00e0 kho\u1ea3ng tr\u1eafng hai byte \u1edf gi\u1eefa chu\u1ed7i kh\u00f4ng b\u1ecb x\u00f3a.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void TrimSample() { String str = &quot; hello world! &quot;; System.out.println(str.trim()); }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nCh\u00e0o th\u1ebf gi\u1edbi!\r\n<\/pre>\n<h2>Thay th\u1ebf m\u1ed9t ph\u1ea7n c\u1ee7a chu\u1ed7i b\u1eb1ng m\u1ed9t chu\u1ed7i kh\u00e1c \u2013 <strong>thay th\u1ebf<\/strong><\/h2>\n<p>Thay th\u1ebf m\u1ed9t ph\u1ea7n c\u1ee7a chu\u1ed7i b\u1eb1ng chu\u1ed7i \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh. thay th\u1ebf thay th\u1ebf t\u1ea5t c\u1ea3 c\u00e1c chu\u1ed7i ph\u00f9 h\u1ee3p.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void thay th\u1ebfSample() { Chu\u1ed7i str = &quot;xin ch\u00e0o th\u1ebf gi\u1edbi!&quot;; System.out.println(str.replace(&quot;l&quot;, &quot;\u00d7&quot;)); }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nanh \u1ea5y\u00d7\u00d7l\u00e0m vi\u1ec7c\u00d7d!\r\n<\/pre>\n<p>thay th\u1ebf \u0111\u1ea7u ti\u00ean ch\u1ec9 thay th\u1ebf chu\u1ed7i kh\u1edbp \u0111\u1ea7u ti\u00ean.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void thayFirstSample() { Chu\u1ed7i str = &quot;xin ch\u00e0o th\u1ebf gi\u1edbi!&quot;; System.out.println(str.replaceFirst(&quot;l&quot;, &quot;\u00d7&quot;)); }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\nhe\u00d7lo th\u1ebf gi\u1edbi!\r\n<\/pre>\n<h2>Tr\u1ea3 v\u1ec1 s\u1ed1 k\u00fd t\u1ef1 trong m\u1ed9t chu\u1ed7i \u2013 <strong>chi\u1ec1u d\u00e0i<\/strong><\/h2>\n<p>Tr\u1ea3 v\u1ec1 s\u1ed1 k\u00fd t\u1ef1 trong m\u1ed9t chu\u1ed7i. String.length() ch\u1ec9 \u0111\u01a1n gi\u1ea3n tr\u1ea3 v\u1ec1 s\u1ed1 l\u01b0\u1ee3ng k\u00fd t\u1ef1.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void lengthSample() { String str = &quot;\u3007\u3007Prefecture\u25a1\u25a1City\u25b3\u25b3Town 10-11&quot;; System.out.println(str.length()); }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\n14\r\n<\/pre>\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n bi\u1ebft s\u1ed1 byte, h\u00e3y s\u1eed d\u1ee5ng String.getBytes().length. K\u1ec3 t\u1eeb Java8, m\u00e3 k\u00fd t\u1ef1 ti\u00eau chu\u1ea9n l\u00e0 UTF8, v\u00ec v\u1eady m\u1ed7i k\u00fd t\u1ef1 c\u00f3 \u0111\u1ed9 r\u1ed9ng \u0111\u1ea7y \u0111\u1ee7 l\u00e0 3 byte.<\/p>\n<h4>m\u00e3 m\u1eabu<\/h4>\n<pre class=\"sample_src\">\r\npublic static void byteLengthSample() { String str = &quot;\u3007\u3007Prefecture\u25a1\u25a1City\u25b3\u25b3Town 10-11&quot;; System.out.println(str.getBytes().length); }\r\n<\/pre>\n<p>K\u1ebft qu\u1ea3 \u0111\u1ea7u ra:<\/p>\n<pre class=\"console\">\r\n32\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>T\u00e1ch m\u1ed9t chu\u1ed7i \u2013 t\u00e1ch T\u00e1ch m\u1ed9t chu\u1ed7i t\u1ea1i m\u1ed9t chu\u1ed7i c\u1ee5 th\u1ec3 (d\u1ea5u ph\u00e2n c\u00e1ch). M\u00e3 m\u1eabu [\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\/vi\/wp-json\/wp\/v2\/posts\/4617"}],"collection":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/comments?post=4617"}],"version-history":[{"count":8,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts\/4617\/revisions"}],"predecessor-version":[{"id":4628,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/posts\/4617\/revisions\/4628"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/media?parent=4617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/categories?post=4617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/vi\/wp-json\/wp\/v2\/tags?post=4617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}