{"id":89407,"date":"2025-12-02T18:00:11","date_gmt":"2025-12-02T12:30:11","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89407"},"modified":"2025-12-02T14:33:26","modified_gmt":"2025-12-02T09:03:26","slug":"java-string-substring","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/","title":{"rendered":"Java String substring()"},"content":{"rendered":"<p>Offering a wide range of features and methods, the Java programming language is one of the major programming languages \u200b\u200bin use today. The list of available data types is one of the most important aspects of any programming language.<\/p>\n<p>Java will classify data types as Primitive and Non-Primitive data types when String is a Non-Primitive type, meaning it refers to an object. Various methods are present in this string object used to perform various string operations. One such method is the substring() function in Java.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Extract a substring from text:\r\nlet text = \"Tech Vidvan!\";\r\nlet result = text.substring(1, 4);<\/pre>\n<h2>What is Java String substring():<\/h2>\n<p>A portion of a string is returned by the Java String class substring(). We pass beginIndex and endIndex number position in the Java substring method where beginIndex is inclusive, and endIndex is exclusive. For example, the initial index begins at 0 while the end index begins at 1. In the Java String, there are two types of subsparing methods.<\/p>\n<h3>Internal implementation substring(int beginIndex):<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String substring(int beginIndex) {    \r\n       if (beginIndex &lt; 0) {    \r\n           throw new StringIndexOutOfBoundsException(beginIndex);    \r\n       }    \r\n       int subLen = value.length - beginIndex;    \r\n       if (subLen &lt; 0) {    \r\n           throw new StringIndexOutOfBoundsException(subLen);    \r\n       }    \r\n       return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);    \r\n   }<\/pre>\n<h3>Internal implementation substring(int beginIndex, int endIndex):<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String substring(int beginIndex, int endIndex)   \r\n{  \r\nif (beginIndex &lt; 0)   \r\n{  \r\nthrow new StringIndexOutOfBoundsException(beginIndex);  \r\n}  \r\nif (endIndex &gt; value.length)   \r\n{  \r\nthrow new StringIndexOutOfBoundsException(endIndex);  \r\n}  \r\nint subLen = endIndex - beginIndex;  \r\nif (subLen &lt; 0)   \r\n{  \r\nthrow new StringIndexOutOfBoundsException(subLen);  \r\n}  \r\nreturn ((beginIndex == 0) &amp;&amp; (endIndex == value.length)) ? this : new String(value, beginIndex, subLen);  \r\n}<\/pre>\n<h3>Java String substring() method example:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class SubstringExample{  \r\npublic static void main(String args[]){  \r\nString s1=\"techvidvan\";  \r\nSystem.out.println(s1.substring(2,4));\r\nSystem.out.println(s1.substring(2));\r\n}}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nch<br \/>\nchvidvan<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong> Parameters<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">start<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Required.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Start position.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">First character is at index 0.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">end<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Optional.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">End position (up to, but not including).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If omitted: the rest of the string.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Return Value:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Type<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Description<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">A string<\/span><\/td>\n<td><span style=\"font-weight: 400;\">A string containing the extracted characters.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Possible Application:<\/h3>\n<p>Substring extraction is used in a wide variety of applications, including prefix and suffix extraction. The name or the denomination alone from a string containing both quantity and currency symbol could be extracted, e.g. to extract Lastname as an example. The latter is explained in more detail below.<\/p>\n<p><strong>The above application shall be implemented in the following way:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan {\r\n    public static void main(String args[])\r\n    {\r\n        String Str = new String(\"Rs 1000\");\r\n        System.out.print(\"The original string is : \");\r\n        System.out.println(Str);\r\n        System.out.print(\"The extracted substring is : \");\r\n        System.out.println(Str.substring(3));\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>The original string is:<\/strong> Rs 1000<br \/>\nT<strong>he extracted substring is:<\/strong> 1000<\/p>\n<h3>Substring in Java:<\/h3>\n<p>Using the provided indices, the substring method in Java can be used to extract a portion of a string and generate a new string.<\/p>\n<p><strong>The substring approach comes in two forms:<\/strong><\/p>\n<ul>\n<li>substring(int StartIndex)<\/li>\n<li>substring(int StartIndex, endIndex)<\/li>\n<\/ul>\n<h3>String substring()<\/h3>\n<p>A new string that is a substring of the original string is what this method returns. The substring stretches from the character at the startIndex supplied all the way to the string&#8217;s conclusion.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String substring(int StartIndex);<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>StartIndex:<\/strong> the begin index, inclusive.<\/li>\n<\/ul>\n<p><strong>Return Value:<\/strong><\/p>\n<ul>\n<li>a new string that is a part of the old string is given back. The substring extends to the end of the original string after the character at the startIndex given.<\/li>\n<\/ul>\n<h3>String substring(StartIndex, endIndex):<\/h3>\n<p>While the second variant enables you to give both the starting and ending indices for a more precise substring extraction, the first variant extracts the substring from a specified starting index to the end of the string.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String substring(int StartIndex, int endIndex);<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>startIndex:<\/strong> The substring&#8217;s starting index, inclusive.<\/li>\n<li><strong>endIndex:<\/strong> The substring&#8217;s exclusive ending index.<\/li>\n<\/ul>\n<p><strong>Return Value:<\/strong><\/p>\n<ul>\n<li>a new string that is a part of the old string is given back. If the second parameter is provided, the substring starts with the character at startIndex and continues up to endIndex &#8211; 1.<\/li>\n<\/ul>\n<h3>Examples:<\/h3>\n<h4>Using substring()<\/h4>\n<p><strong>The following example uses substring() to display characters from the string &#8220;Techvidvan&#8221;:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const anyString = \"Techvidvan\";\r\nconsole.log(anyString.substring(0, 1));\r\nconsole.log(anyString.substring(1, 0)); \r\nconsole.log(anyString.substring(0, 6)); \r\nconsole.log(anyString.substring(4)); \r\nconsole.log(anyString.substring(4, 7)); \r\nconsole.log(anyString.substring(7, 4)); \r\n\r\nconsole.log(anyString.substring(0, 7));\r\nconsole.log(anyString.substring(0, 10));<\/pre>\n<p><strong>Output:<\/strong><br \/>\nT<br \/>\nT<br \/>\nTechvi<br \/>\nvidvan<br \/>\nvid<br \/>\nvid<br \/>\nTechvid<br \/>\nTechvidvan<\/p>\n<h3>Using substring() with length property:<\/h3>\n<p>For the extraction of the last characters of a string, the following example uses substring() and length properties. Given you don&#8217;t have to know the starting and end indices like in the examples below, this method can be more easily remembered.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const text = \"Techvidvan\";\r\nconsole.log(text.substring(text.length - 4)); \r\nconsole.log(text.substring(text.length - 5));<\/pre>\n<p><strong>Output:<\/strong><br \/>\ndvan<br \/>\nidvan<\/p>\n<h3>Replaces the substring in a string :<\/h3>\n<p>A substring of a string is replaced by the following example. It&#8217;s replacing both single characters and substrings. The Brave New World string is converted to the Brave New Web by a function call at the end of this example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function replaceString(oldS, newS, fullS) {\r\n  for (let i = 0; i &lt; fullS.length; ++i) {\r\n    if (fullS.substring(i, i + oldS.length) === oldS) {\r\n      fullS =\r\n        fullS.substring(0, i) +\r\n        newS +\r\n        fullS.substring(i + oldS.length, fullS.length);\r\n    }\r\n  }\r\n  return fullS;\r\n}\r\nreplaceString(\"Hello\", \"World\", \"Hi TechVidvan\");<\/pre>\n<h3>Applications of substring() Method:<\/h3>\n<p>Replaces the substring in a string A substring of a string is replaced by the following example. It&#8217;s replacing both single characters and substrings. The Brave New World string is converted to the Brave New Web by a function call at the end of this example.<\/p>\n<h3>Conclusion<\/h3>\n<p>A string is a very important data type, and the substring method simply returns part of the original string. Once the subprobation method is called, it creates a new string with an offset and a number. We have given several examples for studying substrings using different methods. Code with relevant screenshots are attached.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Offering a wide range of features and methods, the Java programming language is one of the major programming languages \u200b\u200bin use today. The list of available data types is one of the most important&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447361,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,273,299,263,250,274,275],"class_list":["post-89407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-substring","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java","tag-learn-java-string-substring","tag-string-substring-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 substring() - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java String substring() Various methods are present in this string object used to perform various string operations.\" \/>\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\/java-string-substring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String substring() - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java String substring() Various methods are present in this string object used to perform various string operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/\" \/>\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=\"2025-12-02T12:30:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-substring.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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String substring() - TechVidvan","description":"Java String substring() Various methods are present in this string object used to perform various string operations.","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\/java-string-substring\/","og_locale":"en_US","og_type":"article","og_title":"Java String substring() - TechVidvan","og_description":"Java String substring() Various methods are present in this string object used to perform various string operations.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-12-02T12:30:11+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-substring.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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String substring()","datePublished":"2025-12-02T12:30:11+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/"},"wordCount":718,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-substring.webp","keywords":["java","java string substring()","Java Tutorial","java tutorial for beginners","Learn Java","learn java string substring()","string substring() in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/","name":"Java String substring() - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-substring.webp","datePublished":"2025-12-02T12:30:11+00:00","description":"Java String substring() Various methods are present in this string object used to perform various string operations.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-substring\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-substring.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-substring.webp","width":1200,"height":628,"caption":"java string substring()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-substring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String substring()"}]},{"@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\/89407","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=89407"}],"version-history":[{"count":5,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89407\/revisions"}],"predecessor-version":[{"id":447958,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89407\/revisions\/447958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447361"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}