How To Find First Character In A String In Java
Check if a Cord Contains Character in Java
- Use String
contains()
Method to Check if a String Contains Character - Use String
indexOf()
Method to Check if a Cord Contains Grapheme - Utilise String
contains()
Along Withif-else
Statement - Coffee Program to Search for Certain Characters Present in a String
This tutorial article will introduce how to check if a string contains a specific character in Coffee. In Java, we use the contains()
method in unlike ways to check the presence of characters in a cord. Let usa discuss this method implementation through diverse examples.
Use String contains()
Method to Check if a String Contains Character
Java String's contains()
method checks for a particular sequence of characters nowadays inside a string. This method returns true
if the specified character sequence is nowadays inside the string, otherwise, it returns fake
. Permit united states follow the beneath instance.
import java.util.*; import java.lang.*; import java.io.*; public class Example1 { public static void primary(Cord[] args) { String str = "Character"; Arrangement.out.println(str.contains("h")); System.out.println(str.contains("Char")); System.out.println(str.contains("ac")); System.out.println(str.contains("v")); System.out.println(str.contains("vl")); } }
Output:
true true true faux false
Please notation, the contains()
method is instance-sensitive. If nosotros try looking for CHA
in our given cord, then the upshot will be imitation
, like below,
import coffee.util.*; import coffee.lang.*; import java.io.*; public class Example { public static void main(Cord[] args) { String str = "Character"; Arrangement.out.println(str.contains("H")); Organisation.out.println(str.contains("CHAR")); System.out.println(str.contains("act")); } }
Output:
false false false
Use String indexOf()
Method to Check if a String Contains Graphic symbol
In this case, we will learn to detect the graphic symbol within a string using the indexOf()
method. The indexOf()
method is unlike from the contains()
method as information technology does not return whatsoever Boolean value. Instead, indexOf()
returns an int
value which is really the index of the substring
within the string
. Let us understand the below example.
import java.util.*; import coffee.lang.*; import java.io.*; public grade Example2 { public static void principal(String[] args) { String str = "Hello Globe!"; if(str.indexOf("World") != -1) { System.out.println("The String "+str+" contains Globe"); } else { System.out.println("The String "+str+"does not contain World"); } } }
Output:
The string Hello Globe! contains World
Apply String contains()
Forth With if-else
Statement
According to the character presence, we are at present aware that the Coffee cord contains()
method returns a Boolean value. For this, nosotros can apply this method in an if-else
conditional statement. Allow us discuss in the below example.
import java.util.*; import java.lang.*; import java.io.*; public class Example3 { public static void main(Cord[] args) { String str = "Hi World!"; if(str.contains("World")) { System.out.println("It is true"); } else { System.out.println("It is false"); } } }
Output:
It is true
Java Program to Search for Certain Characters Present in a String
This last example volition make a generic Java program to search certain characters present within a string or not. In that case, we will execute a loop throughout the cord length to find the lucifer for the graphic symbol set. Let us check the below example.
import java.util.*; import java.lang.*; import java.io.*; public class Example4 { public static void chief(Cord[] args) { String str = "yellow"; char[] charSearch = {'y','e','due west'}; for(int i=0; i<str.length(); i++) { char chr = str.charAt(i); for(int j=0; j<charSearch.length; j++) { if(charSearch[j] == chr) { Organization.out.println("Char Value "+charSearch[j]+" is present in "+str); } } } } }
Output:
Char Value y is present in xanthous Char Value e is nowadays in yellow Char Value westward is present in xanthous
Write for u.s.a.
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.
Related Commodity - Java String
How To Find First Character In A String In Java,
Source: https://www.delftstack.com/howto/java/how-to-check-if-a-string-contains-character-in-java/
Posted by: wigginscolusay.blogspot.com
0 Response to "How To Find First Character In A String In Java"
Post a Comment