Java determines whether a string contains a character

Source: Internet
Author: User

First, contains method

1: Description

The Java.lang.String.contains () method returns True if and only if this string contains a specified sequence of char values

2: Statement

Public Boolean contains (Charsequence s)
3: Return value

This method returns true, or False if this string is contained.

4: Example

public static void Main (string[] args) {
        String str = "abc";
        Boolean status = Str.contains ("a");
        if (status) {
            System.out.println ("included");
        } else{
            System.out.println ("not included");
        }
    
Second, indexof method

1: Description

The purpose of java.lang.String.indexOf () is to find the position of a word in a string, as well as to determine whether a character is contained in a string.

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 ("string str contains substring" a "" +result1);
        } else{
            System.out.println ("String str does not contain substring" a "" +result1);
        }
    


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.