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,050 points
73 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 parth295
• 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,880 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,880 points
271 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 in Java by Sirajul
• 38,420 points
190 views
0 votes
3 answers

How to reverse a string in java?

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

answered Aug 16, 2018 in Java by samarth295
• 2,190 points
92 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,880 points
119 views
0 votes
2 answers

Result of character addition in Java

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

answered Aug 22 in Java by Sirajul
• 38,420 points
65 views
0 votes
1 answer

Encode String to UTF-8

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

answered May 28, 2018 in Java by Rishabh
• 3,540 points
50 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,540 points
2,403 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,050 points
606 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 30, 2018 in Java by Sushmita
• 6,880 points
46 views