{"id":307,"date":"2023-02-01T09:34:00","date_gmt":"2023-02-01T09:34:00","guid":{"rendered":"http:\/\/ebhor.com\/?p=307"},"modified":"2023-02-01T15:39:52","modified_gmt":"2023-02-01T15:39:52","slug":"java-indexof-and-last-indexof","status":"publish","type":"post","link":"https:\/\/www.ebhor.com\/java-indexof-and-last-indexof\/","title":{"rendered":"indexof and lastindexof Java Examples"},"content":{"rendered":"
public class StringIndexOf {\n\n public static void main(String[] args) {\n String sentence = \"This is a ball. its color is black\";\n System.out.println(\"first index of \\\"l\\\" is \" + sentence.indexOf('l'));\n System.out.println(\"first index of \\\"l\\\" after position 15 is \" + sentence.indexOf('l', 15));\n int i = sentence.lastIndexOf('l');\n System.out.println(\"last index of \\\"l\\\" is \" + i);\n System.out.println(\"last index of \\\"l\\\" is \" + sentence.lastIndexOf('l',23));\n }\n}\n<\/pre>\nindexOf(int ch)<\/strong> returns int value that is the first occurrence of specified character in given string. Counting start from 0. If character not found it returns -1
\nindexOf(int ch, int fromIndex)<\/strong> returns int value that is the first occurrence of specified character in given string after specified position. If character not found it returns -1.
\nlastIndexOf(int ch)<\/strong>return int value that is last occurrence of specified character.
\npublic int lastIndexOf(int ch,int fromIndex)<\/strong> Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index<\/p>\n","protected":false},"excerpt":{"rendered":"public class StringIndexOf { public static void main(String[] args) { String sentence = “This is a ball. its color is black”; System.out.println(“first index of \\”l\\” is ” + sentence.indexOf(‘l’)); System.out.println(“first index of \\”l\\” after position 15 is ” + sentence.indexOf(‘l’, 15)); int i = sentence.lastIndexOf(‘l’); System.out.println(“last index of \\”l\\” is ” + i); System.out.println(“last index … Read more<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"yoast_head":"\n
indexof and lastindexof Java Examples - Ebhor.com<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n