How to Check if a String Contains a Value in Android Java

How to Test if a String Contains one or More Values in Android Java

Sometimes you will need to check a string for a specific value in android java for your android app. for example, you have a website URL Link and you want check if that link contains a specific word or characters, for that purpose, you can try the code.

For example- www.zidsworld.com,  you can check if this www.zidsworld.com contains “zid”. yes it does, right? and if it does, you can do some action or load it in a new window or browser etc.




If it doesn’t contain “zid” , then you can do another action too.

 

The Code

 

if (string.contains("value 1 here") || string.contains("value 2 here") || string.contains("value 3"))

 

Here is an example code for checking single value in a string, this code checks if the string “www.zidsworld.com” contains “zid”. and if it contains it, it will show a toast, it will show another toast if it doesn’t. this is just an example.

 

String myvalue = "www.zidsworld.com";
           
              if (myvalue.contains("zid")) {

                  Toast.makeText(getApplicationContext(), "The string contains zid", Toast.LENGTH_SHORT).show();

              }
          
              else {
                 Toast.makeText(getApplicationContext(), "The string doesn't contains zid", Toast.LENGTH_SHORT).show();
              }

 

Comments are welcome.




Related Posts

Fix Plugin [id: ‘com.android.application’, version: ‘7.1.3’, apply: false] was not found in any of the following sources

Here is how to fix Plugin [id: ‘com.android.application’, version: ‘7.1.3’, apply: false] was not found in any of the following sources This error is caused by mismatching…

Get Age from Date of Birth in Java Android

Here is how to get a user’s age from date of birth If you need get a user’s age from a given date of birth string, this…

How to Check if String Array Contains a Specific String in Android

Here is how to get a specific value or String from Array in Android To check if a string array contains a specific string, you can use…

Make Android WebView Support Razorpay Payment

How to Support Razorpay Payment and UPI in Android WebView? Android webview is inexpensive and easy way to make apps for websites. But to support some specific…

Make Android Webview Support PayTM Google Pay PhonePe UPI

GooglePay, Phonpe and PayTM support in Android WebView If you have built an android app for your new ecommerce website or any other website that needs accept…

This Post Has One Comment

Leave a Reply

Your email address will not be published.