{"id":1919,"date":"2022-09-12T19:35:07","date_gmt":"2022-09-12T19:35:07","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1919"},"modified":"2022-09-12T19:45:10","modified_gmt":"2022-09-12T19:45:10","slug":"java-string-indexof","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/java-string-indexof\/","title":{"rendered":"Java String indexOf() Method"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>Java String indexOf<\/strong><\/p>\n\n\n<p>Well, with this method, we are getting the first occurrence (index) of the string that we are providing as an argument. This is like saying \u2013 hey&#8230; take this string, and tell me where this string has first occurred in the specified string. Just give me the index of the first occurrence of this particular string.<\/p>\n\n\n\n<p>If you feel confused, just let&#8217;s consider an example to get a clearer picture of this method &#8211;<\/p>\n\n\n\n<p>if we have a String str1 = &#8221;This food house serves good food&#8221;;<br>so, if we are using the indexOf method here, and give the string &#8221;food&#8221; as an argument to this method, we get an output as 5, which is the starting index of &#8221;food&#8221;. Now if you are wondering that there were 2 food there, then why did it pick up the first one? Well, this is what it does. It is searching for the first occurrence of the given string.<\/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.indexOf(&#8220;food&#8221;));<br>}<br>}<\/p>\n\n\n\n<p>If we try to execute the program, we get an output as 5, which is the index where the word food starts. You can manually check this out by starting counting from 0 to 5, and at 5, starts food.<\/p>\n\n\n\n<p>But let&#8217;s consider one more case, that the string you are searching for does not exist there. Like if in the above string, I search for &#8221;burger&#8221;, which is never there. What would happen in this case? Let&#8217;s see &#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.indexOf(&#8220;burger&#8221;));<br>}<br>}<\/p>\n\n\n\n<p>If we try executing this program, we find the output to be -1. so, a -1 just says that the string you are searching for isn&#8217;t present.&nbsp;<\/p>\n\n\n\n<p class=\"has-background-background-color has-background\">One more thing, that we can also find the index of some character instead of a string. So, lets now see how can we do that &#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.indexOf(&#8216;h&#8217;));<br>}<br>}<\/p>\n\n\n\n<p>So, in the above program, we are just finding the first occurrence of the character &#8216;h&#8217;. Just by seeing the string, we can tell that hey&#8230; the character &#8216;h&#8217; first occurred at index 1.<\/p>\n\n\n\n<p>Again here also, if we try to search for such a character that is not at all present in the string, then this also returns -1.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java String indexOf Well, with this method, we are getting the first occurrence (index) of the string that we are providing as an argument. This is like saying \u2013 hey&#8230; take this string, and tell me where this string has first occurred in the specified string. Just give me the index of the first occurrence [&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-1919","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\/1919","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=1919"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1919\/revisions"}],"predecessor-version":[{"id":3006,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1919\/revisions\/3006"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}