Java method to determine whether a string contains a certain character:
1. contains method
1: Description
java The .lang.String.contains() method returns true if and only if this string contains the specified char value sequence
2: Statement
public boolean contains(CharSequence s)
3: Return value
If this string contains, return true, otherwise return false.
4: Example
public static void main(String[] args) { String str = "abc"; boolean status = str.contains("a"); if(status){ System.out.println("包含"); }else{ System.out.println("不包含"); } }
2. indexOf method
1: Description
The purpose of java.lang.String.indexOf() is to Find the position of a word in a string, and also determine whether a string contains a certain character.
2: Statement
int indexOf(int ch,int fromIndex)
3: Return value
The return value of indexOf is int
4: Instance
public static void main(String[] args) { String str1 = "abcdefg"; int result1 = str1.indexOf("a"); if(result1 != -1){ System.out.println("字符串str中包含子串“a”"+result1); }else{ System.out.println("字符串str中不包含子串“a”"+result1); } }
More For java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of How to determine whether a string contains a certain character in java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft