{"id":13522,"date":"2020-10-25T17:49:36","date_gmt":"2020-10-26T00:49:36","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=13522"},"modified":"2023-12-01T04:03:24","modified_gmt":"2023-12-01T12:03:24","slug":"java-substring","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/java-substring\/","title":{"rendered":"Java Substring: A How-To Guide"},"content":{"rendered":"\n<p><em>The Java substring() method returns a portion of a particular string. This method accepts the start and end index values of the characters you would like to retrieve from a string. The original string on which substring() is applied is left unchanged. <\/em><\/p>\n\n\n\n<p>You may want to retrieve a particular part of a string in a Java program.<\/p>\n\n\n\n<p>That\u2019s where the Java <em>substring()<\/em> method comes in. The <em>substring()<\/em> method returns a substring from a string starting from a specific index value and ending at an optional index value.<\/p>\n\n\n\n<p>This tutorial will explore how to use the <em>substring()<\/em> method in Java, and explore a few examples of the string method in a program. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Substring Syntax<\/h2>\n\n\n\n<p>The string substring() method retrieves a sequence of characters from a string. These characters are retrieved based on their index values.<\/p>\n\n\n\n<p>The substring() method is applied to the end of a string like so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>public String substring(startIndex, endIndex)<\/pre><\/div>\n\n\n\n<p>substring() accepts two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> is the index position from which the substring should begin, and is inclusive (required)<\/li><li><strong>end<\/strong> is the index position at which the substring should end and is exclusive. By default, the end value is equal to the length of a string. (optional)<\/li><\/ul>\n\n\n\n<p><a href=\"https:\/\/careerkarma.com\/blog\/java-string\/\">Strings in Java<\/a> are indexed. This means that you can retrieve individual characters from a particular string.<\/p>\n\n\n\n<p>For instance, you can retrieve the first character in a string by referencing its <a href=\"https:\/\/careerkarma.com\/blog\/java-indexof\/\">Java index number<\/a>, which is always 0.<\/p>\n\n\n\n<p>Index numbers are assigned starting from 0 and increase for every new character in a string, including spaces, letters, numbers, and symbols.<\/p>\n\n\n\n<p>An <em>StringIndexOutOfBoundsException<\/em> is raised if you specify an index value that is greater than the length of the string with which you are working. You must make sure that you retrieve the correct part of a string to avoid this error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Substring() Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">String Substring in Java with No End Value<\/h3>\n\n\n\n<p>Say that we are operating a coffee shop. After an order is placed, a string is created in a computer program. This string stores the order number, name of the coffee, and the customer\u2019s name.\n\n<\/p>\n\n\n\n<p>We want to create a program that retrieves every character in our string after the order number. The order number is three characters long. This information is given to the barista, who will brew the coffee order.\n\n<\/p>\n\n\n\n<p>To retrieve the characters after the order number, we can use the <em>substring()<\/em> method. Because we are retrieving all characters after a specific one, we do not need to specify an <em>end<\/em> parameter:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>public class GetOrderData {\n\tpublic static void main(String[] args) {\n\t\tString order = new String(&quot;020 Latte Eileen&quot;);\n\t\tSystem.out.println(order.substring(3));\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns: <em>Latte Eileen<\/em>. We can see our string extends to the character at the end of the string.<\/p>\n\n\n\n<p>We define a public class called <em>GetOrderData. <\/em>This class retrieves the coffee a customer has ordered and their name. We <a href=\"https:\/\/careerkarma.com\/blog\/java-variables\/\">define a variable<\/a> that stores the order.<em> <\/em>This lets us we refer to the customer&#8217;s order throughout our code.<\/p>\n\n\n\n<p>Then, we use the <em>substring()<\/em> method to retrieve every character after the index value 3. This is inclusive of index position 3.<\/p>\n\n\n\n<p>We finally use the <em>printIn()<\/em> method to print out the customer\u2019s name and coffee order to the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">String Substring in Java with an End Value<\/h3>\n\n\n\n<p>Let\u2019s say that we only want to retrieve the order number for the coffee, which is three numbers long. This is displayed on a screen to indicate the customer\u2019s order is about to be processed. <\/p>\n\n\n\n<p>The order number exists at the start of the order string. The order number is three characters long. We can use <em>substring()<\/em> to retrieve the order number:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>public class GetOrderNumber {\n\tpublic static void main(String[] args) {\n\t\tString order = new String(&quot;020 Latte Eileen&quot;);\n\t\tSystem.out.println(order.substring(0, 3));\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Our code returns the following: <code>020<\/code>.<\/p>\n\n\n\n<p>We use the <em>substring()<\/em> method to retrieve the characters between the index positions 0 and 3 in our string. These characters contain the order number that we want to retrieve.\n\n<\/p>\n\n\n\n<p>Remember, string indexes start at 0. The <em>end<\/em> parameter is exclusive of the final value you specify (in this case, 3). This is why we have specified the index values 0 to 3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The substring() method extracts characters from an existing string. substring() accepts start and end index values at which the substring should start and end, respectively. This method creates a new string. It does not modify an existing string.<\/p>\n\n\n\n<p>You\u2019re now equipped with the knowledge you need to use the Java <em>substring()<\/em> method with confidence! Read our guide on <a href=\"https:\/\/careerkarma.com\/blog\/how-to-code-in-java\/\">how to learn Java<\/a> for information on what you may want to learn next.<\/p>\n","protected":false},"excerpt":{"rendered":"The Java substring() method returns a portion of a particular string. This method accepts the start and end index values of the characters you would like to retrieve from a string. The original string on which substring() is applied is left unchanged. You may want to retrieve a particular part of a string in a&hellip;","protected":false},"author":240,"featured_media":12305,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17289],"tags":[],"class_list":{"0":"post-13522","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Java","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java Substring: A How-To Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The Java substring() method is used to return a range of characters from a larger string. Learn how the substring() method works in this article.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/java-substring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Substring: A How-To Guide\" \/>\n<meta property=\"og:description\" content=\"The Java substring() method is used to return a range of characters from a larger string. Learn how the substring() method works in this article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/java-substring\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-26T00:49:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:03:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"668\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Java Substring: A How-To Guide\",\"datePublished\":\"2020-10-26T00:49:36+00:00\",\"dateModified\":\"2023-12-01T12:03:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/\"},\"wordCount\":724,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/java-substring\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/java-substring\/\",\"name\":\"Java Substring: A How-To Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg\",\"datePublished\":\"2020-10-26T00:49:36+00:00\",\"dateModified\":\"2023-12-01T12:03:24+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The Java substring() method is used to return a range of characters from a larger string. Learn how the substring() method works in this article.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/java-substring\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg\",\"width\":1000,\"height\":668},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-substring\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/careerkarma.com\/blog\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Substring: A How-To Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Substring: A How-To Guide | Career Karma","description":"The Java substring() method is used to return a range of characters from a larger string. Learn how the substring() method works in this article.","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:\/\/careerkarma.com\/blog\/java-substring\/","og_locale":"en_US","og_type":"article","og_title":"Java Substring: A How-To Guide","og_description":"The Java substring() method is used to return a range of characters from a larger string. Learn how the substring() method works in this article.","og_url":"https:\/\/careerkarma.com\/blog\/java-substring\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-10-26T00:49:36+00:00","article_modified_time":"2023-12-01T12:03:24+00:00","og_image":[{"width":1000,"height":668,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/java-substring\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Java Substring: A How-To Guide","datePublished":"2020-10-26T00:49:36+00:00","dateModified":"2023-12-01T12:03:24+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-substring\/"},"wordCount":724,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/java-substring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/java-substring\/","url":"https:\/\/careerkarma.com\/blog\/java-substring\/","name":"Java Substring: A How-To Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg","datePublished":"2020-10-26T00:49:36+00:00","dateModified":"2023-12-01T12:03:24+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The Java substring() method is used to return a range of characters from a larger string. Learn how the substring() method works in this article.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/java-substring\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/02\/photography-of-person-typing-1181675-2.jpg","width":1000,"height":668},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/java-substring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/careerkarma.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"Java Substring: A How-To Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=13522"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13522\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/12305"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=13522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=13522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=13522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}