Java substring 0 indexof

Java String indexOf() Method Examples

Java String indexOf() method is used to get the first index of a character or a substring in this string.

String indexOf() Methods

There are four overloaded indexOf() methods in String class.

Java String IndexOf Methods

  1. indexOf(int ch): returns the index of the first occurrence of the given character. If the character is not found, then this method returns -1.
  2. indexOf(int ch, int fromIndex): returns the index of the first occurrence of the given character, starting the search from the given index. The method returns -1 if the given character is not found. If the fromIndex is negative, it’s considered as 0. If the fromIndex value is greater than the length of the string, -1 is returned.
  3. indexOf(String str): returns the index of the first occurrence of the given substring. If the substring is not found, then -1 is returned.
  4. indexOf(String str, int fromIndex): returns the index of the first occurrence of the given substring, starting from the given index. The method returns -1 if the substring is not found or the fromIndex is greater than the length of the string. If the fromIndex is negative, it’s considered as 0.

Java String indexOf() Method Examples

Let’s look at some simple examples of all the indexOf() methods.

1. indexOf(int ch)

jshell> String str = "Hello World"; str ==> "Hello World" jshell> str.indexOf('o') $18 ==> 4 jshell> str.indexOf('O') $20 ==> -1 jshell> str.indexOf(111) $24 ==> 4 jshell> str.indexOf('\u006F') $25 ==> 4

Java String IndexOf Examples

The code point value of character ‘o’ is 111 and it can also be written in Unicode as ‘\u006F’.

Читайте также:  Write page in php

2. indexOf(int ch, int fromIndex)

jshell> String str = "Hello World"; str ==> "Hello World" jshell> str.indexOf('o', 5) $32 ==> 7 jshell> str.indexOf('o', 8) $33 ==> -1 jshell> str.indexOf('o', -5) $34 ==> 4 jshell> str.indexOf('o', 50) $35 ==> -1

Java String IndexOf FromIndex Examples

3. indexOf(String str)

jshell> String str = "Hello JavaString"; str ==> "Hello JavaString" jshell> str.indexOf("Hell") $37 ==> 0 jshell> str.indexOf("Hello") $38 ==> 0 jshell> str.indexOf("Java") $39 ==> 6 jshell> str.indexOf("123") $40 ==> -1

Java String IndexOf Substring Examples

4. indexOf(String str, int fromIndex)

jshell> String str = "Hello Jello"; str ==> "Hello Jello" jshell> str.indexOf("ll", 5) $42 ==> 8 jshell> str.indexOf("ll", -5) $43 ==> 2 jshell> str.indexOf("ll", 50) $44 ==> -1 jshell> str.indexOf("xyz", 0) $45 ==> -1

Getting All the indexes of a Substring

There is no method defined to get all the indexes of the occurrence of a substring. However, we can easily write the code to get all the indexes of the occurrences of a substring.

package net.javastring.strings; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class JavaStringIndexOf < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.println("Please enter a line:"); String input = scanner.nextLine(); System.out.println("Please enter a substring:"); String substring = scanner.nextLine(); scanner.close(); int index = 0; Listindexes = new ArrayList<>(); while (index < input.length()) < int i = input.indexOf(substring, index); if (i == -1) break; indexes.add(i); index = i + 1; >System.out.println("The substring found at indexes: " + indexes); > >

Java String IndexOf All Occurrences

String indexOf() vs contains()

  • The String contains() method is used to test if the specified substring is present in this string or not. The String indexOf() method is used to get the index of the first occurrence of a character or a substring in this string.
  • The contains() method internally uses indexOf() method. It’s implemented as return indexOf(s.toString()) >= 0 .
  • We can use the indexOf() method as a replacement of the contains() method. But from a readability point of view, use contains() method to test the presence of a substring.

References:

Источник

indexOf in Java – How to Find the Index of a String in Java

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Читайте также:  Основы PHP и MySQL

indexOf in Java – How to Find the Index of a String in Java

A string is a collection of characters nested in double quotes. The indexOf method returns the index position of a specified character or substring in a string.

In this article, we’ll see the syntax for the different indexOf methods. We’ll also look at some examples to help you understand and use them effectively to find the index of a character or substring in your Java code.

Syntax for the indexOf Method

The indexOf method has the following methods:

public int indexOf(int char) public int indexOf(int char, int fromIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex)

Let’s explain these parameters before seeing some examples:

  • char represents a single character in a string.
  • fromIndex signifies the position where the search for the index of a character or substring should begin. This is important where you have two characters/strings that have the same value in a string. With this parameter, you can tell the indexOf method where to start its operation from.
  • str represents a substring in a string.

Don’t worry if you don’t yet understand how any of this works – the examples will make it all clear!

How to Use the indexOf Method in Java

In the first example below, we’ll find the index of a single character in a string. This example will help us understand the public int indexOf(int char) method.

indexOf(int Char) Method Example

In the code above, we got the index of the character «0» returned to us which is 4. We have two «o» characters but the index of the first one got returned.

In the next example, we’ll see how we can return the index of the second «o» in the next example.

If you’re wondering how the index numbers are derived then you should note that the first character in a string has an index of zero, the second character has an index of one, and so on.

indexOf(int Char, Int fromIndex) Method Example

Here’s an example that explains the int indexOf(int char, int fromIndex) method:

Читайте также:  Питон сколько раз встречается буква

In the example above, we are telling the indexOf method to begin its operation from the fifth index.

Note that index 5 is not the character «W». The fifth index is the space between «Hello» and «World».

So from the code above, every other character that comes before the fifth index will be ignored. 7 is returned as the index of the second «o» character.

Int indexOf(String Str) Method Example

In the next example, we’ll understand how the public int indexOf(String str) method which returns the index of a substring works.

Wondering how we got 11 returned? You should check the last section to understand how indexes are counted and how spaces between substrings count as indexes as well.

Note that when a substring is passed in as a parameter, the index returned is the index of the first character in the substring – 11 is the index of the «b» character.

indexOf(String Str, Int fromIndex) Method Example

The last method – public int indexOf(String str, int fromIndex) – is the same as the public int indexOf(int char, int fromIndex) method. It returns an index from a specified position.

In the example above, we have specified that the method should start its operation from the fifth index which is the index that comes after the first «for» substring. 21 is the index of the second «for» substring.

Lastly, when we pass in a character or substring that doesn’t exist in a string, the indexOf method will return a value of -1. Here is an example:

Conclusion

In this article, we learned how to use the four indexOf methods with an example explaining each of the different methods.

We also saw what the syntax for each of these methods looks like and how they are able to tell the index to return.

We ended by showing what happens when a character or substring that doesn’t exist is passed in as a parameter.

Источник

Оцените статью