

{"id":126619,"date":"2024-07-25T18:00:09","date_gmt":"2024-07-25T12:30:09","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=126619"},"modified":"2026-05-18T12:07:46","modified_gmt":"2026-05-18T06:37:46","slug":"java-string-lastindexof-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/","title":{"rendered":"Java String lastIndexOf() Method with Examples"},"content":{"rendered":"<p>The lastIndexOf() method in Java is an extremely useful method for searching within strings. It allows you to find the last occurrence of a character or substring within a larger string.<\/p>\n<p>The String class in Java has four overloaded variants of the lastIndexOf() method to handle different use cases. These variants give you control over what you are searching for (character or substring) and where to start the search from.<\/p>\n<h3>Java lastIndexOf() Method<\/h3>\n<p>The lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it. The search begins from the end of the string and goes backwards. This allows you to conveniently find the last match instead of needing to search from the start.<\/p>\n<p><strong>Some key points about the lastIndexOf() method:<\/strong><\/p>\n<ul>\n<li>It is overloaded with 4 variants to handle different parameters<\/li>\n<li>The search is done backwards from the end of the string<\/li>\n<li>It returns the index where the match is found<\/li>\n<li>If no match is found, it returns -1<\/li>\n<\/ul>\n<p><strong>The 4 overloaded variants allow you to:<\/strong><\/p>\n<ul>\n<li>Search for a single character<\/li>\n<li>Search for a character with a starting index<\/li>\n<li>Search for a substring<\/li>\n<li>Search for a substring with a starting index<\/li>\n<\/ul>\n<h4>lastIndexOf(char ch) method in Java<\/h4>\n<p>The first variant allows searching for the last occurrence of a character within the string. This method gives the index of the last appearing character in a string. There is one parameter passed to this method in the example, which is the character that we searched for.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public int lastIndexOf(char ch)<\/pre>\n<p>The only parameter it takes is the character to search for. Let&#8217;s see an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class LastIndexOfExample {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int result = str.lastIndexOf('o');\r\n\r\n        System.out.println(\"The last index of 'o' in the string is: \" + result);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>The last index of &#8216;o&#8217; in the string is:<\/strong> 7<\/p>\n<p>Here, we are calling lastIndexOf() on str to find the last occurrence of the letter &#8216;o&#8217;. This would return 7, which is the index of &#8216;o&#8217; in &#8220;World&#8221;.<\/p>\n<h4>lastIndexOf(char ch, int fromIndex) method in Java<\/h4>\n<p>The second variant allows specifying an index to start the backwards search. In this method, two parameters are passed, one of character type and another of integer type. The parameter of integer type represents the index from which it starts searching for the character backwards from the last occurrence.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public int lastIndexOf(char ch, int fromIndex)<\/pre>\n<p><strong>The parameters are:<\/strong><\/p>\n<ul>\n<li><strong>ch &#8211;<\/strong> The character to search for<\/li>\n<li><strong>fromIndex &#8211;<\/strong> The index to start the search backwards from<\/li>\n<\/ul>\n<p><strong>Example of Java lastIndexOf(char ch, int fromIndex):<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class LastIndexOfExample {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int result = str.lastIndexOf('o', 6);\r\n\r\n        System.out.println(\"The last index of 'o' before index 6 is: \" + result);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>The last index of &#8216;o&#8217; before index 6 is:<\/strong> 4<\/p>\n<p>Here, we are searching backwards from index 6 for the last &#8216;o&#8217;. This would return 4, which is the index of the &#8216;o&#8217; in &#8220;Hello&#8221;.<\/p>\n<h4>lastIndexOf(String substring) method in Java<\/h4>\n<p>This variant allows you to search for the last occurrence of a substring within the string. It returns the starting index of the last appearing substring in the string. In this, a string is passed to a method, not a single character. Its syntax is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public int lastIndexOf(String substring)<\/pre>\n<p><strong>Example of Java lastIndexOf(String substring):<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class LastIndexOfExample {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int result = str.lastIndexOf(\"World\");\r\n\r\n        System.out.println(\"The last index of 'World' in the string is: \" + result);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>The last index of &#8216;World&#8217; in the string is:<\/strong> 6<\/p>\n<p>This would return 6, which is the starting index of the substring &#8220;World&#8221;.<\/p>\n<h4>lastIndexOf(String substring, int fromIndex) method in Java<\/h4>\n<p>The last variant combines a substring search with specifying the starting index for a backwards search.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public int lastIndexOf(String substring, int fromIndex)<\/pre>\n<p><strong>The parameters are:<\/strong><\/p>\n<ul>\n<li><strong>substring &#8211;<\/strong> The substring to search for<\/li>\n<li><strong>fromIndex &#8211;<\/strong> The index to start search backwards from<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class LastIndexOfExample {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World. Hello All.\";\r\n        int result = str.lastIndexOf(\"Hello\", 15);\r\n\r\n        System.out.println(\"The last index of 'Hello' before index 15 is: \" + result);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>The last index of &#8216;Hello&#8217; before index 15 is:<\/strong> 13<\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, the lastIndexOf() method in Java proves to be an invaluable asset for searching within strings. With its four overloaded variants, it empowers you to efficiently locate the final instance of a character or substring, all while granting the flexibility to initiate the search from a specified starting point. This adaptability and control enhance its utility in various string manipulation tasks, simplifying the process of finding and extracting information from strings, whether it&#8217;s character-based or substring-based searches, ultimately improving your string-handling capabilities in Java applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The lastIndexOf() method in Java is an extremely useful method for searching within strings. It allows you to find the last occurrence of a character or substring within a larger string. The String class&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":134328,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[7345,31210,31213,31078,31216,31215,8152,31212],"class_list":["post-126619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-lastindexof-method","tag-java-string-lastindexof-method-with-examples","tag-java-tutorials","tag-lastindexof-method","tag-lastindexof-method-in-java","tag-learn-java","tag-string-lastindexof-method"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String lastIndexOf() Method with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"The Java lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String lastIndexOf() Method with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The Java lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-25T12:30:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-18T06:37:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-lastindexof.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=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\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":"Java String lastIndexOf() Method with Examples - DataFlair","description":"The Java lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it.","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:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String lastIndexOf() Method with Examples - DataFlair","og_description":"The Java lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it.","og_url":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-07-25T12:30:09+00:00","article_modified_time":"2026-05-18T06:37:46+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-lastindexof.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java String lastIndexOf() Method with Examples","datePublished":"2024-07-25T12:30:09+00:00","dateModified":"2026-05-18T06:37:46+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/"},"wordCount":643,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-lastindexof.webp","keywords":["Java","java string lastIndexOf() method","java string lastIndexOf() method with examples","java tutorials","lastIndexOf method","lastIndexOf method in java","Learn Java","string lastIndexOf() method"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/","url":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/","name":"Java String lastIndexOf() Method with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-lastindexof.webp","datePublished":"2024-07-25T12:30:09+00:00","dateModified":"2026-05-18T06:37:46+00:00","description":"The Java lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-lastindexof.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-lastindexof.webp","width":1200,"height":628,"caption":"java string lastindexof()"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-string-lastindexof-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java String lastIndexOf() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=126619"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126619\/revisions"}],"predecessor-version":[{"id":148325,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126619\/revisions\/148325"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/134328"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=126619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=126619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=126619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}