{"id":3747,"date":"2017-07-28T03:37:21","date_gmt":"2017-07-28T03:37:21","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3747"},"modified":"2024-09-04T07:23:17","modified_gmt":"2024-09-04T07:23:17","slug":"string-methods-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/string-methods-in-java\/","title":{"rendered":"String methods in Java"},"content":{"rendered":"<h2>String methods in Java<\/h2>\n<p>In this post, we will learn basic String class methods that we use frequently when we deal with Strings in Java. Some of the methods are:<\/p>\n<ul>\n<li><strong>length()<\/strong><\/li>\n<li><strong>substring()<\/strong><\/li>\n<li><strong>trim()<\/strong><\/li>\n<\/ul>\n<p>String basic handling can be found here: <strong><a href=\"https:\/\/www.testingdocs.com\/questions\/string-handling-in-java\/\">https:\/\/www.testingdocs.com\/questions\/string-handling-in-java\/<\/a><\/strong><\/p>\n<h3>length()<\/h3>\n<p>This method returns the length of the String. In other words, this method returns the number of characters ( Unicode code units) in the String.<\/p>\n<p>For example:<\/p>\n<p>String str=&#8221;abc&#8221;;<\/p>\n<p>str.length() would return 3.<\/p>\n<h3>substring()<\/h3>\n<p><strong>substring()<\/strong> String method will extract a portion of the original string. Let&#8217;s write a simple code to demonstrate the usage of the method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial;\r\n\r\npublic class StringDemo {\r\n  public static void main(String[] args) {\r\n    String str=\"abcdefghijklmnopqrstuvwxyz\"; \r\n    String newStr=str.substring(0,5);\r\n    System.out.println(\"Original String str= \"+ str);\r\n    System.out.println(\"Substring of str, newStr = \"+ newStr); \r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3748\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Substring-demo.png\" alt=\"String Substring demo\" width=\"1736\" height=\"1031\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Substring-demo.png 1736w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Substring-demo-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Substring-demo-1024x608.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Substring-demo-768x456.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Substring-demo-1536x912.png 1536w\" sizes=\"auto, (max-width: 1736px) 100vw, 1736px\" \/><\/p>\n<h3><\/h3>\n<h3>trim()<\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial;\r\n\r\npublic class StringDemo {\r\n\r\n  public static void main(String[] args) {\r\n    \/\/Adding 4 white spaces\r\n    String str=\"abcdefghijklmnopqrstuvwxyz    \"; \r\n    String newStr=str.trim();\r\n    System.out.println(\"Original String str= \"+ str);\r\n    System.out.println(\"Original String Length= \"+ str.length());\r\n    System.out.println(\"Trimmed String of str, newStr = \"+ newStr); \r\n    System.out.println(\"Trimmed String Length= \"+ newStr.length());\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>String methods in Java In this post, we will learn basic String class methods that we use frequently when we deal with Strings in Java. Some of the methods are: length() substring() trim() String basic handling can be found here: https:\/\/www.testingdocs.com\/questions\/string-handling-in-java\/ length() This method returns the length of the String. In other words, this method [&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-3747","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\/3747","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=3747"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3747\/revisions"}],"predecessor-version":[{"id":24074,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3747\/revisions\/24074"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}