{"id":89382,"date":"2024-04-04T18:00:13","date_gmt":"2024-04-04T12:30:13","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89382"},"modified":"2024-04-04T18:00:13","modified_gmt":"2024-04-04T12:30:13","slug":"substring-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/","title":{"rendered":"Substring in Java"},"content":{"rendered":"<p>The substring() method, which returns a new string that is a substring of the original string, has two versions. The substring extends from the character at the given index to the last character of the string. The substring&#8217;s endindex starts at 1, not 0.<\/p>\n<p>Java the built-in substring() function of the String class allows one to extract a substring from a given string using index values that are specified as arguments. StartIndex is inclusive and endIndex is exclusive for the substring() technique.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String substring(int begIndex)<\/pre>\n<p><strong>Two methods are followed by a substring:<\/strong><\/p>\n<p>Public String substring(int startIndex) returns a new String object with the given string&#8217;s substring starting at startIndex (inclusive). When the startIndex is greater than the String&#8217;s length or lower than zero, the procedure throws an IndexOutOfBoundException.<\/p>\n<p>Public String substring(int startIndex, int endIndex) creates a new String object using the given string&#8217;s substring from startIndex to endIndex. When the startIndex is less than zero, when the startIndex exceeds the endIndex, or when the endIndex exceeds the length of the String, the function raises an IndexOutOfBoundException.<\/p>\n<p><strong>In case of String:<\/strong><\/p>\n<ul>\n<li><strong>startIndex:<\/strong> inclusive<\/li>\n<li><strong>endIndex:<\/strong> exclusive<\/li>\n<\/ul>\n<h2>Program:<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvan {\n    public static void main(String[] args) {\n        String originalString = \"Hello, World!\";\n        String substring1 = originalString.substring(0, 5);\n        System.out.println(\"Substring 1: \" + substring1);\n        String substring2 = originalString.substring(7);\n        System.out.println(\"Substring 2: \" + substring2);\n        char singleChar = originalString.charAt(4);\n        System.out.println(\"Single Character at index 4: \" + singleChar);\n    }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>Substring 1:<\/strong> Hello<br \/>\n<strong>Substring 2:<\/strong> World!<br \/>\n<strong>Single Character at index 4:<\/strong> o<\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>Define the <strong>originalString<\/strong> variable, which contains the text &#8220;Hello, World!&#8221;.<\/p>\n<p>Use the<strong> substring()<\/strong> method to extract a substring from index 0 (inclusive) to index 4 (exclusive), which extracts &#8220;Hello&#8221; from the originalString. This substring is stored in the <strong>substring1<\/strong> variable and printed.<\/p>\n<p>Use the <strong>substring()<\/strong> method to extract a substring starting at index 7 (inclusive) to the end of the string. This extracts &#8220;World!&#8221; from the<strong> originalString<\/strong>. This substring is stored in the <strong>substring2<\/strong> variable and printed.<\/p>\n<p>Use the <strong>charAt()<\/strong> method to retrieve the character at index 4 in the<strong> originalString<\/strong>, which is &#8216;o&#8217;. This character is stored in the <strong>singleChar<\/strong> variable and printed.<\/p>\n<h3>Using String.split() method:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.*;  \npublic class TechVidvan \n{    \npublic static void main(String args[])  \n{    \nString text= new String(\"Hello, My name is Sachin\");  \nString[] sentences = text.split(\"\\\\.\");  \nSystem.out.println(Arrays.toString(sentences));  \n}  \n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n[Hello, My name is Sachin]<\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>We used the split() technique in the program above. It receives the parameter., which separates the string into another string after checking whether an is present in the phrase. It is kept in an array of sentences that are String objects.<\/p>\n<h3>String substring(begIndex, endIndex):<\/h3>\n<p>This method returns a new string that is a substring of this string and has two variations. The substring starts with the character located at the supplied index and continues up to endIndex &#8211; 1 if the second argument is provided.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String substring(int begIndex, int endIndex);<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>beginIndex:<\/strong> the begin index, inclusive.<\/li>\n<li><strong>endIndex:<\/strong> the end index, exclusive.<\/li>\n<\/ul>\n<p><strong>Return Value:<\/strong><\/p>\n<ul>\n<li>The specified substring.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvan {\n    public static void main(String args[])\n    {\n        String Str = new String(\"Welcome to TechVidvan\");\n        System.out.print(\"The extracted substring is : \");\n        System.out.println(Str.substring(10, 16));\n    }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>The extracted substring is:<\/strong> TechV<\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>O(n), where n is the length of the original string, is the time complexity. The substring is returned by the substring() method in constant time O(1).<\/p>\n<p><strong>Space Complexity:<\/strong> O(1), as the substring operation uses no additional storage.<\/p>\n<h3>Summary<\/h3>\n<p>In conclusion, Java&#8217;s substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks within a longer string, which is helpful for a variety of string manipulation jobs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The substring() method, which returns a new string that is a substring of the original string, has two versions. The substring extends from the character at the given index to the last character of&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":89456,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5432,5433,299,250,5434],"class_list":["post-89382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-substring","tag-java-substring-with-examples","tag-java-tutorial","tag-learn-java","tag-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>Substring in Java - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java&#039;s substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks.\" \/>\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\/substring-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Substring in Java - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java&#039;s substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/substring-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-04-04T12:30:13+00:00\" \/>\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":"Substring in Java - TechVidvan","description":"Java's substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks.","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\/substring-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Substring in Java - TechVidvan","og_description":"Java's substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks.","og_url":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-04-04T12:30:13+00:00","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\/substring-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Substring in Java","datePublished":"2024-04-04T12:30:13+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/"},"wordCount":501,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#primaryimage"},"thumbnailUrl":"","keywords":["java Substring","java substring with examples","Java Tutorial","Learn Java","Substring in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/","name":"Substring in Java - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#primaryimage"},"thumbnailUrl":"","datePublished":"2024-04-04T12:30:13+00:00","description":"Java's substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/substring-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/substring-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Substring in Java"}]},{"@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\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89382","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89382"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89382\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}