How to find out a single character appears in String or not in Java

0 votes
How can we check whether a single character is present in String or not without using Loops?
May 29, 2018 in Java by Daisy
• 8,140 points
5,716 views

2 answers to this question.

0 votes
String str="Hello";
System.out.println(str.indexOf("l"));

This str.indexOf() method is used to check the index of the passed character/letter. If the letter/character is present in the string then it will return non-negative value. If the letter/ character is not present in the String, it will return -1.

answered May 29, 2018 by Parth
• 4,640 points
0 votes

You can use string.indexOf('s').

If the 's' is present in string, it returns the index(>=0). If not, it returns -1. So, a non-negative return value means that 's' is present in the string

answered Aug 7, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,920 points
4,078 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,310 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
1,569 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,920 points
1,431 views
0 votes
2 answers

Result of character addition in Java

Binary arithmetic operations on char and byte ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
4,694 views
0 votes
1 answer

Encode String to UTF-8

String objects in Java use the UTF-16 ...READ MORE

answered May 29, 2018 in Java by Rishabh
• 3,620 points
1,393 views
0 votes
1 answer

Java URL encoding of query string parameters

I would not use URLEncoder. Besides being incorrectly ...READ MORE

answered Jun 1, 2018 in Java by Rishabh
• 3,620 points
17,557 views
0 votes
2 answers

How can we Split Java String by New Line?

If you don’t want empty lines: String.split("[\\r\\n]+") READ MORE

answered Aug 22, 2018 in Java by Daisy
• 8,140 points
7,141 views
0 votes
1 answer
0 votes
2 answers

How to find out current working directory in Java?

You can also use java.nio.file.Path and java.nio.file.Paths. Path ...READ MORE

answered Jul 31, 2018 in Java by Sushmita
• 6,920 points
1,005 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP