{"id":89390,"date":"2025-05-06T18:00:09","date_gmt":"2025-05-06T12:30:09","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89390"},"modified":"2025-05-06T18:04:22","modified_gmt":"2025-05-06T12:34:22","slug":"string-indexof-method-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/","title":{"rendered":"Java String indexOf() Method with Examples"},"content":{"rendered":"<p>Java is a simple programming language that may be used in a variety of applications. The String indexof() function is commonly used in Java. It mainly used in get substring in String and in index of character.<\/p>\n<h2>Explanation<\/h2>\n<p>The indexOf() function returns the location in a string of the first occurrence of the provided character(s).<\/p>\n<p>The lastIndexOf function returns the position of the final occurrence of a string&#8217;s supplied character(s).<strong>\u00a0<\/strong><\/p>\n<h3>There are 4 indexOf() methods in Java:<\/h3>\n<ul>\n<li>public int indexOf(String str)<\/li>\n<li>public int indexOf(String str, int fromIndex)<\/li>\n<li>public int indexOf(int char)<\/li>\n<li>public int indexOf(int char, int fromIndex<\/li>\n<\/ul>\n<p><strong>Parameter:<\/strong><\/p>\n<ul>\n<li><strong>Str &#8211;<\/strong> A String value that represents the string to be searched for fromIndex.<\/li>\n<li><strong>fromindex &#8211;<\/strong> An int integer representing the index position from which to begin the search from<\/li>\n<li><strong>Char-<\/strong>An int value that represents a single character, such as &#8216;A&#8217;, or a Unicode value<\/li>\n<\/ul>\n<h3>Technical Details:<\/h3>\n<p><strong>Return value:<\/strong><\/p>\n<ul>\n<li>An int value reflecting the index of the character&#8217;s first occurrence in the string, or -1 if it never occurs.<\/li>\n<\/ul>\n<p><strong>Behaviour:<\/strong><\/p>\n<ul>\n<li>The indexof() method searches the supplied Substring beginning at the beginning of the string.<\/li>\n<li>When the substring is found, it returns the index of the substring&#8217;s first character.<\/li>\n<li>When the substring cannot be found, it returns -1.<\/li>\n<li>When &#8220;fromIndex&#8221; is used, it searches before or after that index. It enables numerous occurrences of the Substring.<\/li>\n<\/ul>\n<p><strong>Method Signature:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(String str)\r\nint indexOf(String str, int fromIndex)<\/pre>\n<h3>Four methods Signature of IndexOf():<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">int indexOf(int ch)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For the specified char value, it returns the index position.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int indexOf(int ch, int fromIndex)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It returns the index position from index for the specified char value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int indexOf(String substring)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For the specified substring, it returns the index position.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int indexOf(String substring, int fromIndex)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For the specified substring, it returns the index position, and from index<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Internal of Indexof():<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public int indexOf(int ch) {  \r\n        return indexOf(ch, 0);  \r\n    }<\/pre>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvan IndexOf{  \r\npublic static void main(String args[]){  \r\nString s1=\"Indexof method is used \";  \r\n  \r\nint index1=s1.indexOf(\"is\");\r\n\r\nint index2=s1.indexOf(\"index\");\r\n\r\nSystem.out.println(index1+\"  \"+index2);\r\n  \r\nint index3 = s1.indexOf(\"is\", 4);\r\n        System.out.println(index3);\r\n\r\n        int index4 = s1.indexOf('s');\r\n        System.out.println(index4);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n8 0<br \/>\n9<br \/>\n3<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class IndexOf {  \r\n    public static void main(String[] args) {  \r\n        String s1 = \"Java is easy to learn\";             \r\n        int index = s1.indexOf(\"easy\");         System.out.println(\"The index of substring \"+index);          \r\n    }  \r\n  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nThe index of Substring 8<\/p>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvanIndexOf {  \r\n    public static void main(String[] args) {      \r\n        String s1 = \"This is indexOf method\";         \r\n          \r\n        int index = s1.indexOf(\"method\", 10);  \r\n        System.out.println(\"index of substring \"+index);  \r\n        index = s1.indexOf(\"method\", 20);  \r\n        System.out.println(\"index of substring \"+index);          \r\n    }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nindex of substring 16<br \/>\nindex of substring -1<\/p>\n<h3>Table:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">IndexOf(Char, Int32)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This function returns the zero-based index of the first instance of the provided Unicode character in this string. The search begins at a given character point<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">IndexOf(String)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The zero-based index of the first occurrence of the provided string in this instance is reported.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">IndexOf(Char)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This function returns the zero-based index of the first instance of the provided Unicode character in this string.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">IndexOf(String, Int32)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The zero-based index of the first occurrence of the provided string in this instance is reported. The search begins at a given character point<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Variants of indexof():<\/strong><\/p>\n<ul>\n<li>int indexOf()<\/li>\n<li>int indexOf(char ch, int strt)<\/li>\n<li>int indexOf(String str)<\/li>\n<li>int indexOf(String str, int strt)<\/li>\n<\/ul>\n<h4>1. int indexOf():<\/h4>\n<p>This function returns the index of the first occurrence of the provided character within this string, or -1 if the character does not occur.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(char ch )<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ch : character.<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class IndexOf{\r\npublic static void main(String[] args) {\t\t\r\n        String s1 = \"We use Strings\";  \t\t\r\n        \/\/ Passing char and index from\r\n        int index = s1.indexOf('e', 5); \/\/Returns the index of this char\r\n        System.out.println(\"index of char \"+index);\t\t\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nindex of char 5<\/p>\n<h4>2. int indexOf(char ch, int strt)<\/h4>\n<p>This function returns the index of the first occurrence of the supplied character within this string, commencing the search at the specified index, or -1 if the character does not occur.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(char ch, int strt)<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ch :a character.\r\nstr : the index to start the search from.<\/pre>\n<h4>3. int indexOf(String str)<\/h4>\n<p>This function returns the index of the first occurrence of the provided substring within this string. If it does not appear as a substring, it returns -1.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(String str)<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">str : a string.<\/pre>\n<h4>4. int indexOf(String str, int strt)<\/h4>\n<p>This function returns the index of the first occurrence of the provided substring within this text. If it does not appear as a substring, it returns -1.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int indexOf(String str, int str)<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">str : a string.<\/pre>\n<h3>Usage of string indexof():<\/h3>\n<ul>\n<li><strong>Find all occurrence-<\/strong>It finds all the occurrences in the Substring in the string<\/li>\n<li><strong>Searching of substring-<\/strong>It mainly searches the first occurrence of Substring<\/li>\n<li><strong>Specify a specific position-<\/strong> It finds the specific position of the substring in the string<\/li>\n<li><strong>Case insensitive &#8211;<\/strong> It acts insensitively in finding uppercase and lowercase in string<\/li>\n<\/ul>\n<h3>Exceptions of Java String indexof():<\/h3>\n<ul>\n<li><strong>Argument-<\/strong> The value of the NullException is null.<\/li>\n<li>The count or startIndex of ArgumentOutOfRangeException is negative.<\/li>\n<li>StartIndex exceeds the length of this string.<\/li>\n<li>Count exceeds the length of this string less startIndex.<\/li>\n<\/ul>\n<h3>Advantages of String indexof() in Java:<\/h3>\n<ul>\n<li>It aids in determining the position of Substring within the string.<\/li>\n<li>It returns to the place of the first occurrence of the substring.<\/li>\n<li>It is a standard method in programming languages that is utilized on a variety of platforms.<\/li>\n<\/ul>\n<h3>Disadvantages of String indexof() in Java:<\/h3>\n<ul>\n<li>It is case-sensitive and will not find substrings in uppercase or lowercase.<\/li>\n<li>It does not return all occurrences in the substring, but only the first occurrence.<\/li>\n<li>It does not handle overlapping substring matches.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>In Java, we learned about the String indexof() method function. It is primarily useful in the basic substring. This type of String function is used in many areas. We have learned all the content of String indexof().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a simple programming language that may be used in a variety of applications. The String indexof() function is commonly used in Java. It mainly used in get substring in String and in&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447272,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5435,5436,5437,327,5538,250,5438,5439,5440,5441],"class_list":["post-89390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-string-indexof","tag-java-string-indexof-method","tag-java-string-indexof-method-with-examples","tag-java-tutorials","tag-java-tutorials-for-beginners","tag-learn-java","tag-string-indexof","tag-string-indexof-in-java","tag-string-indexof-method","tag-string-indexof-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String indexOf() Method with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java String indexOf() Method is primarily useful in the basic substring. This type of String function is used in many areas.\" \/>\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\/string-indexof-method-in-java\/\" \/>\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 - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java String indexOf() Method is primarily useful in the basic substring. This type of String function is used in many areas.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/\" \/>\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=\"2025-05-06T12:30:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-06T12:34:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/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=\"@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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String indexOf() Method with Examples - TechVidvan","description":"Java String indexOf() Method is primarily useful in the basic substring. This type of String function is used in many areas.","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\/string-indexof-method-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Java String indexOf() Method with Examples - TechVidvan","og_description":"Java String indexOf() Method is primarily useful in the basic substring. This type of String function is used in many areas.","og_url":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-05-06T12:30:09+00:00","article_modified_time":"2025-05-06T12:34:22+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-String-indexOf.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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String indexOf() Method with Examples","datePublished":"2025-05-06T12:30:09+00:00","dateModified":"2025-05-06T12:34:22+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/"},"wordCount":797,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-String-indexOf.webp","keywords":["java string indexof()","java string indexOf() method","java string indexOf() method with examples","java tutorials","java tutorials for beginners","Learn Java","string indexof()","string indexof() in java","string indexOf() method","string indexOf() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/","name":"Java String indexOf() Method with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-String-indexOf.webp","datePublished":"2025-05-06T12:30:09+00:00","dateModified":"2025-05-06T12:34:22+00:00","description":"Java String indexOf() Method is primarily useful in the basic substring. This type of String function is used in many areas.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-String-indexOf.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/04\/java-String-indexOf.webp","width":1200,"height":628,"caption":"java string indexOf()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/string-indexof-method-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String indexOf() 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\/89390","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=89390"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89390\/revisions"}],"predecessor-version":[{"id":447833,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89390\/revisions\/447833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447272"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}