Home > Java > JavaBase > How to determine whether a string contains a certain character in java

How to determine whether a string contains a certain character in java

王林
Release: 2019-11-26 16:32:14
Original
9694 people have browsed it

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)
Copy after login

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("不包含");
        }
    }
Copy after login

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)
Copy after login

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);
        }
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template