{"id":3751,"date":"2017-07-28T03:37:58","date_gmt":"2017-07-28T03:37:58","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3751"},"modified":"2021-03-21T06:20:18","modified_gmt":"2021-03-21T06:20:18","slug":"string-handling-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/string-handling-in-java\/","title":{"rendered":"String Handling in Java"},"content":{"rendered":"<h3>What is a String?<\/h3>\n<p>A String is a sequence of characters and the String class is contained in standard <strong>java.lang<\/strong> package. Using the object of this class we can manipulate the series if characters. Strings are constant and immutable in Java. String values cannot be changed after they are created. If we need mutable Strings we need to use the <strong>StringBuffer<\/strong> class.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3762\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-class-in-Java.png\" alt=\"String class in Java\" width=\"1737\" height=\"1034\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-class-in-Java.png 1737w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-class-in-Java-300x179.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-class-in-Java-1024x610.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-class-in-Java-768x457.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-class-in-Java-1536x914.png 1536w\" sizes=\"auto, (max-width: 1737px) 100vw, 1737px\" \/><\/p>\n<h3><\/h3>\n<h3>Create a String<\/h3>\n<p>The simple way to create a String is using the below declaration.<\/p>\n<p><strong>String str= &#8220;TestingDocs&#8221;;<\/strong><\/p>\n<p>Another approach for creating a string object is:<\/p>\n<p><strong>String s = new String(&#8220;TestPlan&#8221;);<\/strong><\/p>\n<h3>Concatenation of Strings<\/h3>\n<p>The concatenation of strings is joining them into a single String. We can use the + operator for concatenating strings.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial;\r\n\r\npublic class StringConcatenationDemo {\r\n\r\n public static void main(String[] args) { \r\nString strOne=\"Testing\"; String strTwo=\"Docs.com\";\r\n \/\/String concatenation operation String \r\nnewStr=strOne + strTwo; \r\nSystem.out.println(\"String one= \"+ strOne);\r\nSystem.out.println(\"String two= \"+ strTwo);\r\nSystem.out.println(\"Concatenated String= \"+ newStr);\r\n } \r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3767\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Concatenation-Diagram.png\" alt=\"String Concatenation Diagram\" width=\"1713\" height=\"866\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Concatenation-Diagram.png 1713w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Concatenation-Diagram-300x152.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Concatenation-Diagram-1024x518.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Concatenation-Diagram-768x388.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Concatenation-Diagram-1536x777.png 1536w\" sizes=\"auto, (max-width: 1713px) 100vw, 1713px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3 class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3763\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-concatenation-in-Java.png\" alt=\"String concatenation in Java\" width=\"1730\" height=\"1028\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-concatenation-in-Java.png 1730w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-concatenation-in-Java-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-concatenation-in-Java-1024x608.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-concatenation-in-Java-768x456.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-concatenation-in-Java-1536x913.png 1536w\" sizes=\"auto, (max-width: 1730px) 100vw, 1730px\" \/><\/h3>\n<h3>concat()<\/h3>\n<p>We can use the <strong>concat()<\/strong> String method to concatenate Strings as shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial;\r\n\r\npublic class StringConcatenationDemo {\r\n\r\n  public static void main(String[] args) {\r\n    String strOne=\"Testing\"; \r\n    String strTwo=\"Docs.com\"; \r\n    System.out.println(\"String one= \"+ strOne);\r\n    System.out.println(\"String two= \"+ strTwo);\r\n    System.out.println(\"Concatenated String= \"+ strOne.concat(strTwo));\r\n  }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-3751\" data-postid=\"3751\" class=\"themify_builder_content themify_builder_content-3751 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>What is a String? A String is a sequence of characters and the String class is contained in standard java.lang package. Using the object of this class we can manipulate the series if characters. Strings are constant and immutable in Java. String values cannot be changed after they are created. If we need mutable Strings [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3751","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3751"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3751\/revisions"}],"predecessor-version":[{"id":19722,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3751\/revisions\/19722"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}