How to determine whether a string contains a certain character in java
1. contains method
1: Description
java.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)
Recommended online related video learning: java online tutorial
3: Return value
This method returns true if this string contains, false otherwise.
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
java.lang.String. The purpose of indexOf()
is to find the position of a word in a string, and it can 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: Example
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); } }
Related articles Tutorial recommendation: java introductory program
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
