How to search characters and substring in a string in java

Searching a character in the String

  • indexOf(char c) : 

    It looks through the record of indicated characters inside a given string. It begins looking from starting to the furthest limit of the string (from left to right) and returns the relating file whenever discovered in any case returns - 1. 

    Note: If the given string contains numerous events of a determined person then it returns a file of just the first event of indicated character.

    Syntax:
int indexOf(char c)
// Accepts character as argument, Returns index of 
// the first occurrence of specified character 
  • lastIndexOf(char c): It begins looking in reverse from the end of the string and returns the file of the determined person at whatever point it is experienced.

         Syntax:

public int lastIndexOf(char c)
// Accepts character as argument, Returns an 
// index of the last occurrence specified 
// character
  • IndexOf(char c, int indexFrom): It begins looking forward from the predefined list in the string and returns the relating file when the predetermined person is experienced in any case returns - 1. Note: The returned record should be more noteworthy than or equivalent to the predefined file.

           Syntax:

public int IndexOf(char c, int indexFrom)
char: character to be searched.
indexfrom : an integer from where searching
// Returns an index of specified character that
// appeared at or after the specified index in 
// forward direction
  • lastIndexOf(char c, int fromIndex): It starts searching backward from the specified index in the string. And returns the corresponding index when the specified character is encountered otherwise returns -1.

         Note: The returned list should be not exactly or equivalent to the predefined file.

Syntax: 

public int lastIndexOf(char c, int fromIndex)
  • charAt(int indexNumber): Returns the person existing at the predetermined record, indexNumber in the given string. In the event that the predetermined record number doesn't exist in the string, the technique tosses an unchecked exemption, StringIndexOutOfBoundsException.

Syntax:

char charAt(int indexNumber)
// Java program to illustrate to find a character
// in the string.
import java.io.*;

class GFG
{
public static void main (String[] args)
{
	// This is a string in which a character
	// to be searched.
	String str = "GeeksforGeeks is a computer science portal";

	// Returns index of first occurrence of character.
	int firstIndex = str.indexOf('s');
	System.out.println("First occurrence of char 's'" +
					" is found at : " + firstIndex);

	// Returns index of last occurrence specified character.
	int lastIndex = str.lastIndexOf('s');
	System.out.println("Last occurrence of char 's' is" +
					" found at : " + lastIndex);

	// Index of the first occurrence of specified char
	// after the specified index if found.
	int first_in = str.indexOf('s', 10);
	System.out.println("First occurrence of char 's'" +
					" after index 10 : " + first_in);

	int last_in = str.lastIndexOf('s', 20);
	System.out.println("Last occurrence of char 's'" +
					" after index 20 is : " + last_in);

	// gives ASCII value of character at location 20
	int char_at = str.charAt(20);
	System.out.println("Character at location 20: " +
											char_at);

	// throws StringIndexOutOfBoundsException
	// char_at = str.charAt(50);
}
}

Output:

First occurrence of char 's' is found at : 4
Last occurrence of char 's' is found at : 28
First occurrence of char 's' after index 10 : 12
Last occurrence of char 's' after index 20 is : 15
Character at location 20: 111

Searching Substring in the String

  • The techniques utilized for looking through a person in the string which is referenced above can likewise be utilized for looking through the substring in the string.
// Java program to illustrate to find a substring
// in the string.
import java.io.*;

class GFG
{
public static void main (String[] args)
{
	// This is a string in which a substring
	// is to be searched.
	String str = "GeeksforGeeks is a computer science portal";

	// Returns index of first occurrence of substring
	int firstIndex = str.indexOf("Geeks");
	System.out.println("First occurrence of char Geeks"+
			" is found at : " + firstIndex);

	// Returns index of last occurrence
	int lastIndex = str.lastIndexOf("Geeks");
	System.out.println("Last occurrence of char Geeks is"+
			" found at : " + lastIndex);

	// Index of the first occurrence
	// after the specified index if found.
	int first_in = str.indexOf("Geeks", 10);
	System.out.println("First occurrence of char Geeks"+
			" after index 10 : " + first_in);

	int last_in = str.lastIndexOf("Geeks", 20);
	System.out.println("Last occurrence of char Geeks " +
			"after index 20 is : " + last_in);
}
}

Output:

First occurrence of char Geeks is found at : 0
Last occurrence of char Geeks is found at : 8
First occurrence of char Geeks after index 10 : -1
Last occurrence of char Geeks after index 20 is : 8
  • contains(CharSequence seq): It returns valid if the String contains the predefined grouping of roast qualities in any case returns bogus. Its boundaries indicate the succession of characters to be looked at and tosses NullPointer exemption if seq is invalid
public boolean contains(CharSequence seq)

Note: CharSequence is an interface that is implemented by String class, Therefore we use string as an argument in contains() method.

// Java program to illustrate how to find a substring
// in the string using contains
import java.io.*;
import java.lang.*;

class GFG
{
public static void main (String[] args)
{
	// This is a string in which substring
	// to be searched.
	String test = "software";

	CharSequence seq = "soft";
	boolean bool = test.contains(seq);
	System.out.println("Found soft?: " + bool);

	// it returns true substring if found.
	boolean seqFound = test.contains("war");
	System.out.println("Found war? " + seqFound);

	// it returns true substring if found otherwise
	// return false.
	boolean sqFound = test.contains("wr");
	System.out.println("Found wr?: " + sqFound);
}
}

Output:

Found soft?: true
Found war? true
Found wr?: false

Matching String Start and End

 

  • boolean startsWith(String str): Returns true if the string str exist at the starting of the given string, else false.
  • boolean startsWith(String str, int indexNum): Returns true if the string str exist at the starting of the index indexNum in the given string, else false.
  • boolean endsWith(String str): Returns true if the string str exist at the ending of the given string, else false.
// Java program to illustrate to match
// of start and end of a substring
import java.io.*;

class GFG
{
public static void main (String[] args)
{
	// This is a string in which substring
	// is to be searched.
	String str = "GeeksforGeeks is a computer science portal";

	System.out.println(str.startsWith("Geek"));
	System.out.println(str.startsWith("is", 14));
	System.out.println(str.endsWith("port"));
}
}

Output:

true
true
false

 

Submit Your Programming Assignment Details