Saturday, November 9, 2019

String Contains() As Well As Indexof() Illustration - Banking Enterprise Fit String For Substring

Some fourth dimension you lot desire to depository fiscal establishment check if 1 String contains roughly other String or SubString or non e.g. "HelloWorld" contains "Hello" as well as "World" SubString. There are many ways to depository fiscal establishment check SubString inwards String inwards Java e.g. contains(), indexOf() or matches() method of java.lang.String class. In our finally article nosotros stimulate got seen how to purpose matches method of String shape amongst regular expression as well as In this article nosotros volition encounter event of contains() as well as indexOf() method to depository fiscal establishment check whatsoever String or SubString inwards Java. contains as well as indexOf method are defined in java.lang.String shape as well as used to depository fiscal establishment check if String contains a item SubString or not, alone departure betwixt contains() as well as indexOf() method is that, contains is added inwards Java five along amongst String matches() method piece indexOf() is every bit onetime every bit equals(), hashCode as well as compareTo(). Another departure betwixt contains() as well as indexOf() method is that contains() supply boolean value e.g. true if String contains SubString as well as false if String doesn't contains SubString simply 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() as well as indexOf() Example inwards Java

Some fourth dimension you lot desire to depository fiscal establishment check if 1 String contains roughly other String or SubString or non e String contains() as well as indexOf() Example - Check String for SubStringLet's encounter 1 quick event of how to purpose contains() as well as indexOf() method of Java String class. Here nosotros volition used them to depository fiscal establishment check if String contains a item SubString or not. In this Java programme nosotros stimulate got used String "Hello World" as well as checking  for give-and-take "Hello" as well as "World" inwards this String.


/**
 *
 * Java programme to depository fiscal establishment check String for SubString. This program
 * demonstrate ii ways to detect if One String contains a item give-and-take or non inwards Java.
 * contains() as well as indexOf() method of String shape is used to depository fiscal establishment check SubString
 * @author
 */

public class StringContainsExample {

 
    public static void main(String args[]) {

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

Output:
Yes 'Hello World' contains give-and-take 'World'
Great 'Hello World' contains give-and-take 'Hello'

If nosotros re run this Java programme 1 time to a greater extent than amongst dissimilar input e.g. checking for Helloo or Woorld SubString which is non inwards master copy String you lot volition have dissimilar output. By the means Another departure betwixt contains() as well as indexOf() method is that contains() bring a CharSequence object every bit parameter which is a super interface for String, StringBuffer as well as StringBuilder inwards Java. You tin mail away exceed either of those object to contains() method inwards Java.

These are ii quick ways to check if 1 String contains other String, Character or Substring or not, which is really mutual programming enquiry inwards Java interviews, along amongst how to compare ii 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 as well as it's nonetheless worth exploring. That's all on String contains() event as well as String indexOf() event inwards Java.

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

No comments:

Post a Comment