{"id":1921,"date":"2022-09-12T19:49:26","date_gmt":"2022-09-12T19:49:26","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1921"},"modified":"2022-09-12T19:49:27","modified_gmt":"2022-09-12T19:49:27","slug":"java-string-lastindexof","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/java-string-lastindexof\/","title":{"rendered":"Java String lastIndexOf() Method"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>Java String lastIndexOf<\/strong><\/p>\n\n\n<p>Well, in some of the above methods, we tried finding the first occurrence index of some string, or some character. If you are wondering that hey&#8230; what if we want to get the last occurrence of some string or some character? How can we do that?<\/p>\n\n\n\n<p>Well, for this also, we have some method here for us, which is the lastIndexOf method. Remember that it takes a string or a character as an argument.<\/p>\n\n\n\n<p>In return, it gives the index of the last occurrence of that string or that character. In this method also, we can find that it returns -1 if the string or the character is not present.<\/p>\n\n\n\n<p>Let&#8217;s have a look at an example program just to get a clear idea &#8211;<\/p>\n\n\n\n<p class=\"has-background-color has-foreground-background-color has-text-color has-background\">public class StringMethods {<br>public static void main(String[] args) {<br>String str1 = &#8220;This food house serves good food&#8221;;<br>System.out.println(str1.lastIndexOf(&#8220;food&#8221;));<br>}<br>}<\/p>\n\n\n\n<p>So, from the above program, we can comfortably understand that we are searching for the last index of the string &#8221;food&#8221;. Executing the program, we find the output to be 28.<\/p>\n\n\n\n<p>Let&#8217;s have a program also for searching for a character in the string &#8211;<\/p>\n\n\n\n<p class=\"has-background-color has-foreground-background-color has-text-color has-background\">public class StringMethods {<br>public static void main(String[] args) {<br>String str1 = &#8220;This food house serves good food&#8221;;<br>System.out.println(str1.lastIndexOf(&#8216;o&#8217;));<br>}<br>}<\/p>\n\n\n\n<p>From the above program, it is clear that we are trying to search for the character &#8216;o&#8217; in the string. Executing the program, we get the output as 30.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java String lastIndexOf Well, in some of the above methods, we tried finding the first occurrence index of some string, or some character. If you are wondering that hey&#8230; what if we want to get the last occurrence of some string or some character? How can we do that? Well, for this also, we have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[12,11],"class_list":{"0":"post-1921","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-java-tutorial","7":"tag-java","8":"tag-java-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=1921"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1921\/revisions"}],"predecessor-version":[{"id":3007,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1921\/revisions\/3007"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}