Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday, 23 July 2021

How Do I Find a Character in a String in Java

 Searching Characters in a String in Java

In the last article, we have learned how do I find a first non-repeated character in string in java and now here we are going to make a java program for how do I find a character in a string with a simple example.

Here we will find the index position of a character given in a java string by using the pre-defined method of a java String Object.

Here we will find the character position in a sting by using the indexOf( ) method of the String class.

So let's start searching for specific character in string.

Find Character in Java String

class FindingCharacterInString
{
public static void main(String args[])
{
String str = "how are you men";
int indexPosition = str.indexOf('a'); //finding character a String
System.out.println("Index Position of character a is  "+indexPosition);
}
}

Output : Index Position of character is 4

Note the above program for finding a character will give you the first occurrence of the character's position not the second occurrence of a character.

Let's understand by simple example where we again find the character in a string but with a different character.

class FindingCharacterInString
{
public static void main(String args[])
{
String str = "how are you men";
int indexPosition = str.indexOf('o'); //finding character String
System.out.println("Index Position of character is  "+indexPosition);
}
}

Output: Index Position of character is 1

So here we discussed some useful examples for how do i find a character in a String in java.
Share:

6 comments:

  1. Hi,
    Thanks for sharing the information. Very informative blog.
    Hire Angularjs Developer

    ReplyDelete
  2. Thank you for sharing useful information with us. please keep sharing like this. And if you are searching a unique and Top University in India, Colleges discovery platform, which connects students or working professionals with Universities/colleges, at the same time offering information about colleges, courses, entrance exam details, admission notifications, scholarships, and all related topics. Please visit below links:

    Manav Rachna International Institute of Research and Studies Faridabad

    NIMS University Jaipur

    Nirma University Ahmedabad

    PES University Bangalore

    Rabindranath Tagore University Bhopal

    ReplyDelete

  3. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete

  4. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete

  5. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate