• Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Check if String contains Substring in Java

Written by Rahul, Updated on May 16, 2019

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.

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 it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Debian 10
  • Download Ubuntu 20.04 LTS – DVD ISO Images
  • Linux Run Commands As Another User
  • How to Check PHP Version (Apache/Nginx/CLI)
  • How To Install and Configure GitLab on Ubuntu 20.04
  • How to Install PyCharm on Ubuntu 20.04
  • How to Check Ubuntu Version with Command or Script
  • How to Set all directories to 755 And all files to 644
© 2013-2021 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy