String Contains() In Addition To Indexof() Event - Cheque String For Substring

Some fourth dimension yous desire to cheque if 1 String contains closed to other String or SubString or non e.g. "HelloWorld" contains "Hello" too "World" SubString. There are many ways to cheque SubString inwards String inwards Java e.g. contains(), indexOf() or matches() method of java.lang.String class. In our in conclusion article nosotros induce got seen how to purpose matches method of String shape amongst regular expression too In this article nosotros volition run into illustration of contains() too indexOf() method to cheque whatever String or SubString inwards Java. contains too indexOf method are defined in java.lang.String shape too used to cheque if String contains a detail SubString or not, alone deviation betwixt contains() too indexOf() method is that, contains is added inwards Java v along amongst String matches() method spell indexOf() is every bit sometime every bit equals(), hashCode too compareTo(). Another deviation betwixt contains() too indexOf() method is that contains() supply boolean value e.g. true if String contains SubString too false if String doesn't contains SubString exactly indexOf() supply int value, which is genuinely index of SubString i.e. starting location. indexOf() supply -1 if source String does non comprise SubString inwards Java.


String contains() too indexOf() Example inwards Java

Some fourth dimension yous desire to cheque if 1 String contains closed to other String or SubString or non e String contains() too indexOf() Example - Check String for SubStringLet's run into 1 quick illustration of how to purpose contains() too indexOf() method of Java String class. Here nosotros volition used them to cheque if String contains a detail SubString or not. In this Java plan nosotros induce got used String "Hello World" too checking  for discussion "Hello" too "World" inwards this String.


/**
 *
 * Java plan to cheque String for SubString. This program
 * demonstrate 2 ways to detect if One String contains a detail discussion or non inwards Java.
 * contains() too indexOf() method of String shape is used to cheque SubString
 * @author
 */

public class StringContainsExample {

 
    public static void main(String args[]) {

       String discussion = "Hello World";
     
       //indexOf supply -1 if String does non comprise specified word
       if(word.indexOf("World") != -1){
           System.err.printf("Yes '%s' contains discussion 'World' %n" , word);
       }else{
           System.err.printf("Sorry %s does non contains discussion 'World' %n " , word);
       }
     
       //contains method supply boolean truthful if String contains specified word
       if(word.contains("Hello")){
           System.err.printf("Great '%s' contains discussion 'Hello' %n" , word);
       }else{
           System.err.printf("Sorry %s does non contains discussion 'Hello' %n" , word);
       }
     
    }
}

Output:
Yes 'Hello World' contains discussion 'World'
Great 'Hello World' contains discussion 'Hello'

If nosotros re run this Java plan in 1 trial again amongst dissimilar input e.g. checking for Helloo or Woorld SubString which is non inwards master copy String yous volition have dissimilar output. By the means Another deviation betwixt contains() too indexOf() method is that contains() convey a CharSequence object every bit parameter which is a super interface for String, StringBuffer too StringBuilder inwards Java. You tin overstep either of those object to contains() method inwards Java.

These are 2 quick ways to check if 1 String contains other String, Character or Substring or not, which is really mutual programming query inwards Java interviews, along amongst how to compare 2 String inwards Java or how to dissever String inwards Java. There are lot of changes inwards Java API from Java 1.5 to Java 1.5 too it's all the same worth exploring. That's all on String contains() illustration too String indexOf() illustration inwards Java.

Further Learning
Data Structures too Algorithms: Deep Dive Using Java
Difference betwixt StringBuffer too String inwards Java

0 Response to "String Contains() In Addition To Indexof() Event - Cheque String For Substring"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel