{"id":89742,"date":"2024-12-17T18:00:33","date_gmt":"2024-12-17T12:30:33","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89742"},"modified":"2024-12-17T18:43:23","modified_gmt":"2024-12-17T13:13:23","slug":"string-trim-method-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/","title":{"rendered":"Java String trim() Method"},"content":{"rendered":"<p>A builtin function that eliminates leading and trailing spaces is the trim function of Java String. A space character&#8217;s Unicode value is &#8220;u0020&#8221;. Before or after a string, this Unicode value is checked by the trim() method in Java and spaces are removed if necessary.<\/p>\n<p>&nbsp;<\/p>\n<h2>Method Signature<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String trim()<\/pre>\n<p><strong>Return Type<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Trim in Java():\r\nimport java.io.*;\r\nclass techvidvan  {\r\npublic static void main(String[] args) {\r\nString x=\"tech \";\r\nString y=\"for \";\r\nString z=\"vidvan\";\r\nSystem.out.println(x+y+z);\r\nSystem.out.println(x.trim()+y.trim()+z.trim());\r\n}}<\/pre>\n<h3>Method signature<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String trim()<\/pre>\n<p><strong>Parameters<\/strong><\/p>\n<p>The trim() method takes no parameters.<\/p>\n<p><strong>Return Type<\/strong><\/p>\n<p>The return type of the trim() method is String. Returns an omitted string without leading and trailing spaces.<br \/>\nBelow are examples of how Java&#8217;s string trim() method works.<\/p>\n<h3>Example for Trim<\/h3>\n<h4>Method signature<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String trim()<\/pre>\n<p><strong>Parameters<\/strong><\/p>\n<p>The trim() method has no parameters.<\/p>\n<p><strong>Return Type<\/strong><\/p>\n<p>The return type of the trim() method is String. Returns an escaped string without leading and trailing spaces.<br \/>\nBelow are examples of how Java&#8217;s string trim() method works.<\/p>\n<p><strong>Parameters<\/strong><\/p>\n<p>None.<\/p>\n<p><strong>Returns:<\/strong><\/p>\n<p>A string value is a copy of the string without leading and trailing spaces.<\/p>\n<h4>Syntax of trim() in Java<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String trim().<\/pre>\n<h3>Example for Trim<\/h3>\n<h4>Example 1<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\nclass techvidvan  {\r\npublic static void main(String[] args) {\r\nString x=\"tech \";\r\nString y=\"for \";\r\nString z=\"vidvan\";\r\nSystem.out.println(x+y+z); with\r\nSystem.out.println(x.trim()+y.trim()+z.trim());\r\n}\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p>tech for vidvan<br \/>\ntechforvidan<\/p>\n<h4>Example 2<\/h4>\n<p>After using the trim function, it returns a string instead of changing the original string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class techvidvan  {\r\npublic static void main(String args[])\r\n{\r\nString s = \" techvidvan  has all Java features to read \";\r\nSystem.out.println(s.trim());\r\nSystem.out.println(s);\r\n}\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p>tech for vidvan has all java functions to read<br \/>\ntech for vidvan has all java functions to read<\/p>\n<h4>Example 3<\/h4>\n<p>Using the trim function, we get two strings the original and the returned string, both differ in the case that we remove spaces from the original string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\nclass techvidvan {\r\npublic static void main(String[] args)\r\n{\r\nString s1 = \"techvidvan \";\r\nSystem.out.println(\"Before Trim() - \");\r\nSystem.out.println(\"String - \" + s1);\r\nSystem.out.println(\"Length - \" + s1.length());\r\nString s2 = s1.trim();\r\nSystem.out.println(\"\\nAfter Trim() - \");\r\nSystem.out.println(\"String - \" + s2);\r\nSystem.out.println(\"Length - \" + s2.length());\r\nif (s1 == s2) {\r\nSystem.out.println(\"\\nEqual\");\r\n}\r\notherwise {\r\nSystem.out.println(\"\\nNot equal to\");\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p>Before Trim() &#8211;<br \/>\nString &#8211; tech for vidvan<br \/>\nLength &#8211; 21<\/p>\n<p>After Trim() &#8211;<br \/>\nString &#8211; tech for vidvan<br \/>\nLength &#8211; 15<\/p>\n<p>Not Equal<\/p>\n<h3>Public String trim()<\/h3>\n<p>To obtain a string with the value of this string, leading and trailing spaces will be removed by using trim() method. A reference to this String object shall be returned if this String object contains an empty sequence of characters or the first and last characters of the sequence of characters represented by this String object have a code greater than &#8216;u0020&#8217;, a space character.<\/p>\n<p>In the case of a string with no characters with code in excess of &#8216;u0020&#8217;, an empty string object is returned. Otherwise, k shall be the index of the first character in a string whose code is greater than &#8216;u0020&#8217;, and m shall be the index of the last character in this string whose code is greater than &#8216;u0020.<\/p>\n<p>This method can be used to trim the spaces listed above from the beginning or end of each string.<\/p>\n<h3>Conclusion<\/h3>\n<p>The Java String trim() method removes lead and trailing spaces. No standard API is used to check if the characters are empty. For a character whose Unicode code falls below or is not equal to U+0020, this deletes it. Therefore, in Java 11, a method called strip() has been added. Use the strip() method if you use Java 11 or higher.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A builtin function that eliminates leading and trailing spaces is the trim function of Java String. A space character&#8217;s Unicode value is &#8220;u0020&#8221;. Before or after a string, this Unicode value is checked by&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447232,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,292,5518,299,263,250,5687,5517,294],"class_list":["post-89742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-trim-method","tag-java-string-trim-method-with-example","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java","tag-string-trim","tag-string-trim-method","tag-string-trim-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String trim() Method - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java String trim() method is used to remove lead and trailing spaces. In checking if the characters are empty or not, no standard API used.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String trim() Method - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java String trim() method is used to remove lead and trailing spaces. In checking if the characters are empty or not, no standard API used.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-17T12:30:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-17T13:13:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-trim.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String trim() Method - TechVidvan","description":"Java String trim() method is used to remove lead and trailing spaces. In checking if the characters are empty or not, no standard API used.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Java String trim() Method - TechVidvan","og_description":"Java String trim() method is used to remove lead and trailing spaces. In checking if the characters are empty or not, no standard API used.","og_url":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-12-17T12:30:33+00:00","article_modified_time":"2024-12-17T13:13:23+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-trim.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String trim() Method","datePublished":"2024-12-17T12:30:33+00:00","dateModified":"2024-12-17T13:13:23+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/"},"wordCount":461,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-trim.webp","keywords":["java","java string trim() method","java string trim() method with example","Java Tutorial","java tutorial for beginners","Learn Java","string trim()","string trim() method","string trim() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/","name":"Java String trim() Method - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-trim.webp","datePublished":"2024-12-17T12:30:33+00:00","dateModified":"2024-12-17T13:13:23+00:00","description":"Java String trim() method is used to remove lead and trailing spaces. In checking if the characters are empty or not, no standard API used.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-trim.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-trim.webp","width":1200,"height":628,"caption":"java String trim()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/string-trim-method-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String trim() Method"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89742"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89742\/revisions"}],"predecessor-version":[{"id":447766,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89742\/revisions\/447766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447232"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}