indexOf() Method is used to get index of the first occurrence of a criteria specified in the parameters of the IndexOf method. A common scenario can be when a system admin wants to find the index of the ‘@’ character of the email Id of a client and then wants to get the remaining substring. In that situation, IndexOf method can be
used. The syntax of Java String indexOf() Method is: Below are the indexOf() Java String parameters: char – Used to represent a single character value str – Used to represent the string to search for fromIndex – Used to represent the index position to start the search from This indexOf()
Java String method returns the index within this string of the first occurrence of the specified character. It returns -1 if the character does not occur. The Java String IndexOf method has four overloads. All the overloads return an integer type value, representing the returned index. These overloads differ in the type and number of parameters they accept. This method returns the index of the character ‘b’ passed as parameter. If that character is not
available in the string, the returned index would be -1. The given method would return the index of the first occurrence of character ‘c’ after the integer index passed as second parameter “startindex.” All the occurrences of character ‘c’ before the “startindex” integer index would be ignored. IndexOf(String substring)The above Java substring indexOf() method returns the index of the first character of the substring passed as a parameter to it. If that substring is not available in the string, the returned index would be -1. IndexOf(String substring, int startindex)This Java substring indexOf() method returns the index of the first character in the substring passed as the first parameter, after the “startindex” index value. If substring starts from the passed integer value of “startindex”, that substring would be ignored. public class Sample_String { public static void main(String args[]) { String str_Sample = "This is Index of Example"; //Character at position System.out.println("Index of character 'x': " + str_Sample.indexOf('x')); //Character at position after given index value System.out.println("Index of character 's' after 3 index: " + str_Sample.indexOf('s', 3)); //Give index position for the given substring System.out.println("Index of substring 'is': " + str_Sample.indexOf("is")); //Give index position for the given substring and start index System.out.println("Index of substring 'is' form index:" + str_Sample.indexOf("is", 5)); } } Output:Index of character ‘x’: 12 11-9-1: What is printed by the following statements? 11-9-2: What will be printed when the following executes? str = "His shirt is red" pos = str.find("is") print(pos) 11-9-3: What will be printed when the following executes? str = "This is fun" str = str[5] print(str) 11-9-4: What is the value of s1 after the following code executes? s1 = "heY" s1 = s1.capitalize() s1.lower() 11-9-5: What would the following code print? Mali = 5 print("Mali" + " is " + str(Mali)) 11-9-6: What is printed by the following statements? s = "python rocks" print(s[3]) 11-9-7: What is printed by the following statements? s = "python is awesome" print(s[2] + s[-5]) 11-9-8: What is printed by the following statements? s = "python rocks" print(len(s)) 11-9-9: What is printed by the following statements: s = "Rose" s[1] = "i" print(s) 11-9-10: What is printed by the following statements: s = "ball" r = "" for item in s: r = item.upper() + r print(r) 11-9-11: What is printed by the following statements? s = "python rocks" print(s[7:11] * 3) 11-9-12: What is printed by the following statements? animal = "dog" print("animal" + animal) 11-9-13: What is printed by the following statements? animal = "dog" print("animal " + "animal") You have attempted of activities on this page Which of the following method call gives the position of X that occurs after n th position in the string S1 S1 Index (' x 1 S1 Index (' x N?S1. indexOf('X', n)
Which of the following method call gives the position of the Firstoccurrence of x in the string S1?The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.
Which of the following functions is used to find the position of the particular substring within a string?The FIND function in Excel is used to return the position of a specific character or substring within a text string.
Which of the following is true about string?A string does not let the user type letters numbers or words as it has already been written. Hence, it is the correct option.
|