{"id":89394,"date":"2024-06-03T18:00:19","date_gmt":"2024-06-03T12:30:19","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89394"},"modified":"2024-06-03T18:21:35","modified_gmt":"2024-06-03T12:51:35","slug":"java-string-lastindexof-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/","title":{"rendered":"Java String lastIndexOf() Method with Examples"},"content":{"rendered":"<p>The lastIndexOf() method gives the index (position) of the string&#8217;s last instance of a given value. The string is traversed beginning at the end via the lastIndexOf() method. The starting index (position 0) is returned by the lastIndexOf() method.<\/p>\n<h2>Definition of Java String lastIndexOf() Method:<\/h2>\n<p>The position of the final instance of the provided character(s) in a string is returned by the lastIndexOf() function. To find the first instance of the supplied character(s) in a string, use the indexOf method.<\/p>\n<p>This method returns the index of the character&#8217;s last occurrence, less than or equal to beg, in the character sequence that this object represents, or -1 if the character doesn&#8217;t appear before that point.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<ul>\n<li>string.lastIndexOf(int ch, int index)<\/li>\n<li>string.lastIndexOf(string str, int index)<\/li>\n<\/ul>\n<h3>There are 4 lastIndexOf() methods:<\/h3>\n<h4>Table:<\/h4>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">public int lastIndexOf(String str)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For the specified substring, it returns the latest index point<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public int lastIndexOf(String str, int fromIndex)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For the specified substring, it returns the last index position, starting at index.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public int lastIndexOf(int char)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The specified char value&#8217;s last index position is returned.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public int lastIndexOf(int char, int fromIndex)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It returns the last index position from index for the specified char value.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Return Value:<\/strong><\/p>\n<p>If the character doesn&#8217;t appear at all, -1; otherwise, an int value denoting the character&#8217;s first occurrence in the string.<br \/>\nThe index is returned by this procedure.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<p>str A String value that represents the string to be searched for in fromIndexAn int integer that represents the starting point for the search. The length of the string char is the default value if absent. A Unicode value or an int value that represents a single character, such as &#8220;A,&#8221;.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Main {\r\n  public static void main(String[] args) {\r\n    String myStr = \"Hello planet earth, you are a great planet.\";\r\n    System.out.println(myStr.lastIndexOf(\"e\", 5));\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n1<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class L_index1 {\r\n \r\npublic static void main(String args[])\r\n    {\r\n \r\n        \/\/ Initialising String\r\n        String Str = new String(\"Welcome to geeksforgeeks\");\r\n \r\n        System.out.print(\"Found Last Index of g at : \");\r\n \r\n        System.out.println(Str.lastIndexOf('g'));\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>Found Last Index of g at <\/strong>19<\/p>\n<h3>Diagram:<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-Lastindexof-diagram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89657\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-Lastindexof-diagram.webp\" alt=\"Java Lastindexof() diagram\" width=\"400\" height=\"251\" \/><\/a><\/p>\n<h3>Case sensitive:<\/h3>\n<p>The indexOf() method returns -1 if the target string cannot be found or the index number where it is first discovered. Because the indexOf() method is case-sensitive, like equals(), uppercase and lowercase characters are treated differently.<\/p>\n<h3>Time complexity:<\/h3>\n<p>The lastIndexOf() function in Java has an O(n) time complexity, where n is the string&#8217;s length. This is the case because the procedure loops through the entire string from beginning to end until it finds the final instance of the requested substring or gets to the beginning of the string.<\/p>\n<h3>Exception:<\/h3>\n<p>The value of the substring is passed as a parameter to the lastIndexOf() function as a string. If the supplied string is null, an exception is thrown. In the event that the substring cannot be discovered, the procedure returns -1.<\/p>\n<h4>Advantages:<\/h4>\n<ul>\n<li>It is a built-in method in Java.<\/li>\n<li>It is efficient and fast.<\/li>\n<li>It can be used to search for any substring.<\/li>\n<\/ul>\n<h4>Disadvantages:<\/h4>\n<ul>\n<li>It can only be used to search for substrings.<\/li>\n<li>If the substring does not occur in the string, the method returns-1.<\/li>\n<li>If the string is very large, The method can be slow.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>This has taught us the syntax for the Java lastIndexOf() function and, with the help of examples, how to utilise it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The lastIndexOf() method gives the index (position) of the string&#8217;s last instance of a given value. The string is traversed beginning at the end via the lastIndexOf() method. The starting index (position 0) is&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447216,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,5506,5507,5508,299,263,250,5510,5509,5511],"class_list":["post-89394","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-lastindexof","tag-java-string-lastindexof-method","tag-java-string-lastindexof-with-example","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java","tag-string-lastindexof-in-java","tag-string-lastindexof-method","tag-string-lastindexof-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String lastIndexOf() Method with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"The Java lastIndexOf() method gives the index (position) of the string&#039;s last instance of a given value. To find the first instance of the supplied character(s) in a string, use the indexOf method.\" \/>\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\/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 - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The Java lastIndexOf() method gives the index (position) of the string&#039;s last instance of a given value. To find the first instance of the supplied character(s) in a string, use the indexOf method.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/\" \/>\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-06-03T12:30:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-03T12:51:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/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=\"@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":"Java String lastIndexOf() Method with Examples - TechVidvan","description":"The Java lastIndexOf() method gives the index (position) of the string's last instance of a given value. To find the first instance of the supplied character(s) in a string, use the indexOf method.","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\/java-string-lastindexof-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String lastIndexOf() Method with Examples - TechVidvan","og_description":"The Java lastIndexOf() method gives the index (position) of the string's last instance of a given value. To find the first instance of the supplied character(s) in a string, use the indexOf method.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-06-03T12:30:19+00:00","article_modified_time":"2024-06-03T12:51:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-lastIndexOf.webp","type":"image\/webp"}],"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\/java-string-lastindexof-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String lastIndexOf() Method with Examples","datePublished":"2024-06-03T12:30:19+00:00","dateModified":"2024-06-03T12:51:35+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/"},"wordCount":516,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-lastIndexOf.webp","keywords":["java","java string lastIndexOf()","java string lastIndexOf() method","java string lastIndexOf() with example","Java Tutorial","java tutorial for beginners","Learn Java","string lastIndexOf() in java","string lastIndexOf() method","string lastIndexOf() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/","name":"Java String lastIndexOf() Method with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-lastIndexOf.webp","datePublished":"2024-06-03T12:30:19+00:00","dateModified":"2024-06-03T12:51:35+00:00","description":"The Java lastIndexOf() method gives the index (position) of the string's last instance of a given value. To find the first instance of the supplied character(s) in a string, use the indexOf method.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-lastIndexOf.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/06\/java-String-lastIndexOf.webp","width":1200,"height":628,"caption":"java String lastIndexOf()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-lastindexof-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String lastIndexOf() Method with Examples"}]},{"@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\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89394","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\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89394"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89394\/revisions"}],"predecessor-version":[{"id":447507,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89394\/revisions\/447507"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447216"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}