Find Character in String in Java

Find character in String in java

In this post, we will see how to find character in String in java.

How to Find Character in String in Java

There are multiple ways to find char in String in java

1. Using indexOf() Method to Find Character in String in Java

indexOf() method is used to find index of substring present in String. It returns 1 if character is present in String else returns -1.

Let’s see with the help of example:

Output:

Index of a in String Java2blog is: 1

In case, you want to find character in String after nth index, then you can pass fromIndex to indexOf method.

2. Using lastIndexOf() Method to Find Char in String in Java

lastIndexOf() method is used to find last index of substring present in String. It returns 1 if character is present in String else returns -1. This method scans String from end and returns as soon as it finds the character.

Output:

Index of a in String Java2blog is: 3

Find Character at Index in String in Java

Using String’s charAt() method, you can find character at index in String in java.

Let’s see with the help of example:

Output:

Character at index 5 in String Java2blog is: b

how Do I Find Word in String in Java?

You can use indexOf() method to find word in String in java. You need to pass word to indexOf() method and it will return the index of word in the String.

Output:

Index of a in String Java2blog is: 5

That’s all about how to find character in String in java.

Was this post helpful?

Related Posts

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in JavaWas this post helpful? In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix […]

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusionWas this post […]

  • Java String contains IgnoreCase
    23 June

    Java String contains Ignore Case

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusionWas this post helpful? We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String […]

  • Find and count occurences of substring in String in Java
    02 June

    Find and count occurrences of substring in string in java

    Table of ContentsUsing the indexOf() methodUsing regular expressionUsing split() methodUsing the countMatches() method from Apache Commons libraryWas this post helpful? In this post, we will see how to find and count occurrences of substring in string in java. Using the indexOf() method The indexOf() method in java is a specialized function to find the index […]

  • Print double quotes in java
    23 May

    Print double quotes in java

    Table of ContentsPrint double quotes in java using backslash characterPrint double quotes in java using unicode characterPrint double quotes in java by appending double quote characterWas this post helpful? 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-62b870281eae7271407703/] When you print on console using System.out.println, you use […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.