Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How to Check if String contains Substring in Java

    How to Check if String contains Substring in Java

    By RahulMay 16, 20191 Min Read

    Question – How do I check If a String contains another substring in Java? In our previous article, you learned to compare two strings in Java programming language. This tutorial will help you to check if the main string contains another substring in it.

    Advertisement

    Syntax

    1
    2
    str1.contains(str2)    // Case sensitive
    str1.toLowerCase().contains(str2.toLowerCase())   // Ignore case

    Example 1 – Check with Case Sensitive

    This will be case sensitive check. As per the below example, Welcome is different than welcome as we know java is a case sensitive programming language.

    1
    2
    3
    4
    String str1 = "Hi, Welcome to TecAdmin";
     
    str1.contains("Welcome")   //Return True
    str1.contains("welcome")   //Return false (due to case sensitive)

    Example 2 – Check with Ignoring Case

    Using this string will be checked with ignoring the case. The function change string to lower case and check if one string contains another case.

    1
    2
    3
    4
    5
    6
    String str1 = "Hi, Welcome to TecAdmin";
    String str2 = "welcome";
     
    //The below code will change all charecters to lower case
    //Then compare strings, but do not change original content.
    str1.toLowerCase().contains(str2.toLowerCase())   //Return true

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Implementing a Linux Server Security Audit: Best Practices and Tools

    15 Practical Examples of dd Command in Linux

    Iptables: Common Firewall Rules and Commands

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Python Lambda Functions – A Beginner’s Guide
    • 10 Practical Use Cases for Lambda Functions in Python
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.