Title: Substring Java Example - Java Code Geeks
Open Graph Title: Substring Java Example - Java Code Geeks
Description: Check out our detailed Substring Java example ! We will show how to use the Java substring() API method, it's syntax and a full example with source code!
Open Graph Description: Check out our detailed Substring Java example ! We will show how to use the Java substring() API method, it's syntax and a full example with source code!
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/substring-java-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/substring-java-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/substring-java-example/"},"author":{"name":"Konstantina Dimtsa","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56"},"headline":"Substring Java Example","datePublished":"2013-12-24T14:15:57+00:00","dateModified":"2022-07-05T20:26:01+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/substring-java-example/"},"wordCount":280,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/substring-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["featured"],"articleSection":["String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/substring-java-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/substring-java-example/","url":"https://examples.javacodegeeks.com/substring-java-example/","name":"Substring Java Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/substring-java-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/substring-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2013-12-24T14:15:57+00:00","dateModified":"2022-07-05T20:26:01+00:00","description":"Check out our detailed Substring Java example ! We will show how to use the Java substring() API method, it's syntax and a full example with source code!","breadcrumb":{"@id":"https://examples.javacodegeeks.com/substring-java-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/substring-java-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/substring-java-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/substring-java-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Java Basics","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/"},{"@type":"ListItem","position":4,"name":"String","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/string-java-basics/"},{"@type":"ListItem","position":5,"name":"Substring Java Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56","name":"Konstantina Dimtsa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","caption":"Konstantina Dimtsa"},"description":"Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.","sameAs":["http://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/konstantina-dimtsa/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Java Basics","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/"}},{"@type":"ListItem","position":4,"item":{"name":"String","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/string-java-basics\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2013-12-24T16:15:57+02:00","datePublished":"2013-12-24T16:15:57+02:00","dateModified":"2022-07-05T23:26:01+03:00","headline":"Substring Java Example","name":"Substring Java Example","keywords":"featured","url":"https:\/\/examples.javacodegeeks.com\/substring-java-example\/","description":"In this post, we feature a comprehensive Substring Java Example. We will show how to use Java String substring() API method. 1. Syntax substring() method can be expressed in two ways: String subs","copyrightYear":"2013","articleSection":"String","articleBody":"\nIn this post, we feature a comprehensive Substring Java Example. We will show how to use Java String substring() API method.\n\n\n\n1. Syntax\n\n\n\nsubstring() method can be expressed in two ways:\n\n\n\nString substring(int start, int end)String substring(int start)\n\n\n\n2. Parameters\n\n\n\nstart\n\n\n\nThis parameter is mandatory and it specifies the starting position of the extracted string. Note that first character of string is at index 0 not 1.\n\n\n\nend\n\n\n\nThis parameter is optional and it specifies the end position of the extraction. Note that the index of end position is not included in the extraction.If end parameter is missing, then the extraction starts from the starting position to the rest of the string.\n\n\n\n You can also check the Java String Class Example in the following video: \n\n\n\nJava String Class Example - Video\n\n\n\n3. Return value\n\n\n\nThe return value of substring() method is the extracted string.\n\n\n\n4. Substring Java Example\n\n\n\nCreate a java class named SubstringExample.java with the following code:\n\n\n\nSubstringExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n\n\n\npublic class SubstringExample {\n \n public static void main(String args[]) {\n String str = new String(\"Javacodegeeks\");\n \n System.out.println(\"Initial string is: \" + str);\n \n System.out.println(\"Start position=4 and no end position specified: \"\n + str.substring(4));\n \n System.out.println(\"Start position=2 and end position=11: \"\n + str.substring(2, 11));\n \n \/\/ if start = end, then the extracted string will be empty\n System.out.println(\"Start position=3 and end position=3: \"\n + str.substring(3, 3).isEmpty());\n \n \/\/ In the following cases we will get\n \/\/ java.lang.StringIndexOutOfBoundException\n \n \/\/ if (start < 0 or end < 0)\n System.out.println(\"Start position=-2 and end position=5: \"\n + str.substring(-2, 5));\n \n \/\/ if (start > end)\n System.out.println(\"Start position=5 and end position=2: \"\n + str.substring(5, 2));\n }\n \n}\n\n\n\n\n\n\n\n\n\n\nOutput\n\n\n\nInitial string is: Javacodegeeks\nStart position=4 and no end position specified: codegeeks\nStart position=2 and end position=11: vacodegee\nStart position=3 and end position=3: true\nException in thread \"main\" java.lang.StringIndexOutOfBoundsException: String index out of range: -2\n\tat java.lang.String.substring(Unknown Source)\n\tat SubstringExample.main(SubstringExample.java:22)\n\n\n\nAs we see in the output, the substring() method usage is exactly what we described in the previous sections. In the first case, end parameter is omitted, so the extracted string starts from position 2 till the rest of the string.In the second case, both parameters are defined.\n\n\n\nIn the third case, we can see that if start position is equal to the end position, then the extracted string is empty.In the last two cases, we can see that if one of the parameters is less than 0 or start position is greater than end position, then we will have StringIndexOutOfBoundsException.\n\n\n\n5. Download the source code\n\n\n\nThis was an example of Substring Java method.\nDownload\nDownload the source code of this example: Substring Java Example\n\n\n\nLast updated on Jan. 09, 2020\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/substring-java-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Konstantina Dimtsa","url":"https:\/\/examples.javacodegeeks.com\/author\/konstantina-dimtsa\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2013-12-24T14:15:57+00:00 |
| article:modified_time | 2022-07-05T20:26:01+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Konstantina Dimtsa |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Konstantina Dimtsa |
| twitter:label2 | Est. reading time |
| twitter:data2 | 3 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1