Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to match a word in Java?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 188
    Comment on it

    Sometimes we need to match some words in a particular String. We can do this easily by matches() method of String class.

    Example:

    package com.demo;
    
    public class TestApps {
    
        /*
         *@param args
         */
        public static void main(String[] args){
    
            String Str = new String("You are welcome to Findnerd.com");
    
            //Matches word in between
            System.out.print("Value :" );
            System.out.println(Str.matches("(.*)Findnerd(.*)"));
    
            System.out.print("Value :" );
            System.out.println(Str.matches("Findnerd"));
    
            //Matches whether the word is last word
            System.out.print("Value :" );
            System.out.println(Str.matches("(.*)com"));
    
            //Matches whether the word is first word
            System.out.print("Value :" );
            System.out.println(Str.matches("You(.*)"));
        }
    }
    

    Output:

    Value :true
    Value :false
    Value :true
    Value :true
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: