How do you index a character in a string?

The indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring. Given a second argument: a number, the method returns the first occurrence of the specified substring at an index greater than or equal to the specified number.

Try it

Syntax

indexOf(searchString)
indexOf(searchString, position)

Parameters

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
0

Substring to search for, coerced to a string.

If the method is called with no arguments,

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
0 is coerced to
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
2. Therefore,
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
3 returns
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
4 — because the substring
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
2 is found at position
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
4 in the string
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
2. But
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
8, returns
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
9 — because the substring
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
2 is not found in the string
"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
1.

"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
2 Optional

The method returns the index of the first occurrence of the specified substring at a position greater than or equal to

"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
2, which defaults to
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
4. If
"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
2 is greater than the length of the calling string, the method doesn't search the calling string at all. If
"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
2 is less than zero, the method behaves as it would if
"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
2 were
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
4.

  • "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    9 returns
    "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    0 — because it causes the method to behave as if the second argument were
    "hello world".indexOf(""); // returns 0
    "hello world".indexOf("", 0); // returns 0
    "hello world".indexOf("", 3); // returns 3
    "hello world".indexOf("", 8); // returns 8
    
    4, and the first occurrence of
    "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    2 at a position greater or equal to
    "hello world".indexOf(""); // returns 0
    "hello world".indexOf("", 0); // returns 0
    "hello world".indexOf("", 3); // returns 3
    "hello world".indexOf("", 8); // returns 8
    
    4 is at position
    "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    0.
  • "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    5 returns
    "hello world".indexOf(""); // returns 0
    "hello world".indexOf("", 0); // returns 0
    "hello world".indexOf("", 3); // returns 3
    "hello world".indexOf("", 8); // returns 8
    
    9 — because, while it's true the substring
    "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    7 occurs at index
    "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    8, that position is not greater than or equal to
    "Blue Whale".indexOf("Blue"); // returns  0
    "Blue Whale".indexOf("Blute"); // returns -1
    "Blue Whale".indexOf("Whale", 0); // returns  5
    "Blue Whale".indexOf("Whale", 5); // returns  5
    "Blue Whale".indexOf("Whale", 7); // returns -1
    "Blue Whale".indexOf(""); // returns  0
    "Blue Whale".indexOf("", 9); // returns  9
    "Blue Whale".indexOf("", 10); // returns 10
    "Blue Whale".indexOf("", 11); // returns 10
    
    9.
  • "Blue Whale".indexOf("blue"); // returns -1
    
    0 returns
    "hello world".indexOf(""); // returns 0
    "hello world".indexOf("", 0); // returns 0
    "hello world".indexOf("", 3); // returns 3
    "hello world".indexOf("", 8); // returns 8
    
    9 — because
    "Blue Whale".indexOf("blue"); // returns -1
    
    2 is greater than the length of
    "Blue Whale".indexOf("blue"); // returns -1
    
    3, which causes the method to not search the string at all.

Return value

The index of the first occurrence of

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
0 found, or
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
9 if not found.

Return value when using an empty search string

Searching for an empty search string produces strange results. With no second argument, or with a second argument whose value is less than the calling string's length, the return value is the same as the value of the second argument:

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8

However, with a second argument whose value is greater than or equal to the string's length, the return value is the string's length:

"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11

In the former instance, the method behaves as if it found an empty string just after the position specified in the second argument. In the latter instance, the method behaves as if it found an empty string at the end of the calling string.

Description

Strings are zero-indexed: The index of a string's first character is

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
4, and the index of a string's last character is the length of the string minus 1.

"Blue Whale".indexOf("Blue"); // returns  0
"Blue Whale".indexOf("Blute"); // returns -1
"Blue Whale".indexOf("Whale", 0); // returns  5
"Blue Whale".indexOf("Whale", 5); // returns  5
"Blue Whale".indexOf("Whale", 7); // returns -1
"Blue Whale".indexOf(""); // returns  0
"Blue Whale".indexOf("", 9); // returns  9
"Blue Whale".indexOf("", 10); // returns 10
"Blue Whale".indexOf("", 11); // returns 10

The indexOf() method is case sensitive. For example, the following expression returns

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
9:

"Blue Whale".indexOf("blue"); // returns -1

Checking occurrences

When checking if a specific substring occurs within a string, the correct way to check is test whether the return value is

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
9:

"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'

Examples

Using indexOf()

The following example uses indexOf() to locate substrings in the string

"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
1.

const str = "Brave new world";

console.log(str.indexOf("w")); // 8
console.log(str.indexOf("new")); // 6

indexOf() and case-sensitivity

The following example defines two string variables.

The variables contain the same string, except that the second string contains uppercase letters. The first

"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
2 method displays
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
3. But because the indexOf() method is case sensitive, the string
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
5 is not found in
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
6, so the second
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
2 method displays
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
9.

const myString = "brie, pepper jack, cheddar";
const myCapString = "Brie, Pepper Jack, Cheddar";

console.log(myString.indexOf("cheddar")); // 19
console.log(myCapString.indexOf("cheddar")); // -1

Using indexOf() to count occurrences of a letter in a string

The following example sets

"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
9 to the number of occurrences of the letter
const str = "Brave new world";

console.log(str.indexOf("w")); // 8
console.log(str.indexOf("new")); // 6
0 in the string
const str = "Brave new world";

console.log(str.indexOf("w")); // 8
console.log(str.indexOf("new")); // 6
1:

Can you use index on a string?

Because strings, like lists and tuples, are a sequence-based data type, it can be accessed through indexing and slicing.

How to use string charAt () in Java?

Java String charAt() Method Examples.
public class CharAtExample{.
public static void main(String args[]){.
String name="javatpoint";.
char ch=name.charAt(4);//returns the char value at the 4th index..
System.out.println(ch);.

How is indexing done in strings?

String indexing in Python is zero-based, so the very first character in the string would have an index of 0 , 00:30 and the next would be 1 , and so on. The index of the last character will be the length of the string minus one.