

{"id":126065,"date":"2024-10-01T18:00:29","date_gmt":"2024-10-01T12:30:29","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=126065"},"modified":"2024-10-01T18:33:02","modified_gmt":"2024-10-01T13:03:02","slug":"java-string-indexof-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/","title":{"rendered":"Java String indexOf() Method with Examples"},"content":{"rendered":"<p>The indexOf() method in Java is a precious resource for handling and searching strings, serving as a means to identify the initial occurrence of a character or substring within a larger string. Its primary function is to provide the position of the first match found or return -1 if no match is detected. This method comes in four distinct variants, each tailored to specific search requirements:<\/p>\n<p>The first variant, indexOf(char ch), is designed to locate the initial instance of a specified character, &#8216;ch,&#8217; within the string. The second variant, indexOf(char ch, int fromIndex), serves a similar purpose but allows for the search to commence from a designated starting index, &#8216;fromIndex.&#8217;<\/p>\n<p>On the other hand, the third variant, indexOf(String str), is employed to identify the first occurrence of a particular substring, &#8216;str.&#8217; Finally, the fourth variant, indexOf(String str, int fromIndex), closely resembles the second variant, as it also permits the search to begin from a user-defined starting index, &#8216;fromIndex.&#8217;<\/p>\n<p>The existence of these various forms of the indexOf() method underscores its flexibility and utility for string searching and manipulation in Java.<\/p>\n<p>This article will explore the syntax and practical application of these indexOf() method variants using instructive examples, providing a comprehensive understanding of their functionality.<\/p>\n<h2>Variants of Java indexOf() Method<\/h2>\n<h3>A. int indexOf(char ch)<\/h3>\n<p>This variant of indexOf() finds the first occurrence of the specified character ch in the string.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(char ch)<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Main {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int index = str.indexOf('o');\r\n        System.out.println(index);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n4<\/p>\n<h3>B. int indexOf(char ch, int fromIndex)<\/h3>\n<p>This overload of indexOf() finds the first occurrence of the specified character ch, starting the search from fromIndex.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(char ch, int fromIndex)<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Main {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int index = str.indexOf('o', 5);\r\n        System.out.println(index);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n7<\/p>\n<h3>C. int indexOf(String substring)<\/h3>\n<p>This variant finds the first occurrence of the specified substring in the string.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(String substring)<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Main {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int index = str.indexOf(\"World\");\r\n        System.out.println(index);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n6<\/p>\n<h3>D. int indexOf(String substring, int fromIndex)<\/h3>\n<p>This overload finds the first occurrence of the specified substring starting the search from fromIndex.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(String substring, int fromIndex)<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Main {\r\n    public static void main(String[] args) {\r\n        String str = \"Hello World\";\r\n        int index = str.indexOf(\"World\", 7);\r\n        System.out.println(index);\r\n    }\r\n}<\/pre>\n<p><strong>Output<\/strong>:<br \/>\n-1<\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, Java&#8217;s indexOf() method offers a versatile and efficient means for string searching and manipulation. Its four distinct variants allow developers to locate characters or substrings within a larger string easily. These variants, including the ability to specify the starting index for the search, add significant flexibility to the method&#8217;s functionality.<\/p>\n<p>By exploring the syntax and examples of each variant, developers can understand how to effectively employ the indexOf() method in their Java programming endeavors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The indexOf() method in Java is a precious resource for handling and searching strings, serving as a means to identify the initial occurrence of a character or substring within a larger string. Its primary&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":126079,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[31191,7345,31190,31186,31188,31078,8152,31189,31187],"class_list":["post-126065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-indexof-method-in-java","tag-java","tag-java-indexof-method","tag-java-string-indexof-method","tag-java-string-indexof-method-with-examples","tag-java-tutorials","tag-learn-java","tag-string-indexof-method","tag-string-indexof-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String indexOf() Method with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"The indexOf() method in Java offers a versatile and efficient means for string searching and manipulation.\" \/>\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-indexof-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String indexOf() Method with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The indexOf() method in Java offers a versatile and efficient means for string searching and manipulation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-string-indexof-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-10-01T12:30:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-01T13:03:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-indexof.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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String indexOf() Method with Examples - DataFlair","description":"The indexOf() method in Java offers a versatile and efficient means for string searching and manipulation.","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-indexof-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String indexOf() Method with Examples - DataFlair","og_description":"The indexOf() method in Java offers a versatile and efficient means for string searching and manipulation.","og_url":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-10-01T12:30:29+00:00","article_modified_time":"2024-10-01T13:03:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-indexof.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java String indexOf() Method with Examples","datePublished":"2024-10-01T12:30:29+00:00","dateModified":"2024-10-01T13:03:02+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/"},"wordCount":397,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-indexof.webp","keywords":["indexOf() method in java","Java","java indexOf() method","java string indexOf() method","java string indexOf() method with examples","java tutorials","Learn Java","string indexOf() method","string indexOf() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/","url":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/","name":"Java String indexOf() Method with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-indexof.webp","datePublished":"2024-10-01T12:30:29+00:00","dateModified":"2024-10-01T13:03:02+00:00","description":"The indexOf() method in Java offers a versatile and efficient means for string searching and manipulation.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-indexof.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-indexof.webp","width":1200,"height":628,"caption":"java string indexof()"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-string-indexof-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 indexOf() 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\/126065","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=126065"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126065\/revisions"}],"predecessor-version":[{"id":143512,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126065\/revisions\/143512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/126079"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=126065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=126065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=126065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}