{"id":8340,"date":"2024-10-25T09:27:36","date_gmt":"2024-10-25T13:27:36","guid":{"rendered":"https:\/\/springframework.guru\/?p=8340"},"modified":"2024-10-25T09:27:39","modified_gmt":"2024-10-25T13:27:39","slug":"how-to-use-the-java-substring-method","status":"publish","type":"post","link":"https:\/\/springframework.guru\/how-to-use-the-java-substring-method\/","title":{"rendered":"How to Use the Java Substring Method"},"content":{"rendered":"\n<p>The <code>substring()<\/code> method in Java is a tool for manipulating strings. It allows you to extract a portion of a string by specifying a start index, and optionally, an end index.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Usage of <code>substring()<\/code><\/h3>\n\n\n\n<p>The Java <code>substring()<\/code> method has two variations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>substring(int startIndex)<\/code>: Extracts from <code>startIndex<\/code> to the end of the string.<\/li>\n\n\n\n<li><code>substring(int startIndex, int endIndex)<\/code>: Extracts characters from <code>startIndex<\/code> to <code>endIndex - 1<\/code>.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"String post = &quot;0123456789012&quot;          \/\/ Character position\nString text = &quot;Hello, World!&quot;;\nString sub1 = text.substring(7);       \/\/ &quot;World!&quot;\nString sub2 = text.substring(0, 5);    \/\/ &quot;Hello&quot;\nSystem.out.println(sub1);              \/\/ Output: World!\nSystem.out.println(sub2);              \/\/ Output: Hello\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">String post <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot;0123456789012&quot;<\/span><span style=\"color: #24292E\">          <\/span><span style=\"color: #6A737D\">\/\/ Character position<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">String text <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot;Hello, World!&quot;<\/span><span style=\"color: #24292E\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">String sub1 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> text.<\/span><span style=\"color: #6F42C1\">substring<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #005CC5\">7<\/span><span style=\"color: #24292E\">);       <\/span><span style=\"color: #6A737D\">\/\/ &quot;World!&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">String sub2 <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> text.<\/span><span style=\"color: #6F42C1\">substring<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #005CC5\">0<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">5<\/span><span style=\"color: #24292E\">);    <\/span><span style=\"color: #6A737D\">\/\/ &quot;Hello&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(sub1);              <\/span><span style=\"color: #6A737D\">\/\/ Output: World!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(sub2);              <\/span><span style=\"color: #6A737D\">\/\/ Output: Hello<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>substring(7)<\/code> extracts <code>\"World!\"<\/code> starting from index 7 until the end.<\/li>\n\n\n\n<li><code>substring(0, 5)<\/code> extracts <code>\"Hello\"<\/code> from index 0 up to (but not including) index 5.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Important Details<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>IndexOutOfBoundsException<\/strong>: Ensure that <code>startIndex<\/code> and <code>endIndex<\/code> are within the string\u2019s length.<\/li>\n\n\n\n<li><strong>Zero-Based Indexing<\/strong>: Java indices start at 0.<\/li>\n\n\n\n<li><strong>Immutable Strings<\/strong>: <code>substring()<\/code> creates a new string without modifying the original.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Substring Use Cases<\/h3>\n\n\n\n<p>The Java <code>substring()<\/code> method is useful in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Parsing specific portions of structured text (e.g., extracting the domain from an email).<\/li>\n\n\n\n<li>Manipulating data in formats such as dates or file paths.<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"String filePath = &quot;\/home\/user\/document.txt&quot;;\nString fileName = filePath.substring(filePath.lastIndexOf(&quot;\/&quot;) + 1); \nSystem.out.println(fileName);   \/\/ Output: document.txt\n\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">String filePath <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot;\/home\/user\/document.txt&quot;<\/span><span style=\"color: #24292E\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">String fileName <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> filePath.<\/span><span style=\"color: #6F42C1\">substring<\/span><span style=\"color: #24292E\">(filePath.<\/span><span style=\"color: #6F42C1\">lastIndexOf<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;\/&quot;<\/span><span style=\"color: #24292E\">) <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">1<\/span><span style=\"color: #24292E\">); <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(fileName);   <\/span><span style=\"color: #6A737D\">\/\/ Output: document.txt<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In the above example, you can see we are using the <code>lastIndexOf<\/code> method to find the last slash character in the <code>filePath<\/code> string. The next and remaining characters are the portion of the <code>filePath<\/code> for the <code>fileName<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>The <code>substring()<\/code> method is a tool for string manipulation.  You can use it to efficiently handle String processing tasks in Java. Be sure to manage indices carefully to avoid exceptions and ensure accurate results in your string manipulations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The substring() method in Java is a tool for manipulating strings. It allows you to extract a portion of a string by specifying a start index, and optionally, an end index. Basic Usage of substring() The Java substring() method has two variations: In this example: Important Details Substring Use Cases The Java substring() method is [&hellip;]<a href=\"https:\/\/springframework.guru\/how-to-use-the-java-substring-method\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":8351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[20],"tags":[27],"class_list":["post-8340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"jt","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2024\/10\/javasubstring.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-2aw","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/8340"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=8340"}],"version-history":[{"count":2,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/8340\/revisions"}],"predecessor-version":[{"id":8352,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/8340\/revisions\/8352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/8351"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=8340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=8340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=8340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}