Built in packages & String Class

Posted on

boolean equals(String str); Compares the contents of two “string” objects.

boolean equalsIgnoreCase(String str); Compare the content of two “string” objects ignores case.

    import java.lang.*;
    class App35
    {
    public static void main(String yck[])
    {
    String s=new String("CHAITANYA");
    String y=new String("chaitanya");
    if(s.equalsIgnoreCase(y))
    System.out.println("The two Strings are equal");
    else
    System.out.println("The two strings are not equal");
    }
    }
    
    
    output:
    
    \>javac App35.java
    \>java App35
    The two Strings are equal
    
    import java.lang.*;
    class extra1
    {
    public static void main(String yck[])
    {
    String s=new String("CHAITANYA");
    String y=new String("chaitanya");
    boolean result;
    result=s.equals(y);
    if(result == true)
    System.out.println("The two Strings are equal");
    else
    System.out.println("The two strings are not equal");
    }
    }
    
    
    output:
    
    \>javac extra1.java
    \>java extra1
    The two strings are not equal

    boolean regionMatches(int startindex, String str1, int str2Startindex, int numChars);

    boolean regionMatches(boolean ignorescase, int startindex, String str, int str2startindex, int numChars); Compares the range of characters in one string object with the range of characters in the another string object.

    str2 : Specifies the destination String being compared to.str2startindex : Specifies the no. of characters to compare.ignorecase : When set to true ignores the case.

      import java.lang.*;
      class App36
      {
      public static void main(String yck[])
      {
      String x=new String("application");
      String y=new String("CATEGORY");
      boolean result;
      result=x.regionMatches(5,y,0,3);
      if(result == true)
      System.out.println("The Region Matches");
      else
      System.out.println("The Region is not Matching");
      }
      }
      
      
      output:
      
      \>javac App36.java
      \>java App36
      The Region is not Matching
      import java.lang.*;
      class extra2
      {
      public static void main(String yck[])
      {
      String x=new String("application");
      String y=new String("CATEGORY");
      boolean result;
      result=x.regionMatches(true,5,y,0,3);
      if(result == true)
      System.out.println("The Region Matches");
      else
      System.out.println("The Region is not Matching");
      }
      }
      
      
      output:
      
      \>javac extra2.java
      \>java extra2
      The Region Matches

      boolean startWith(String str); Determine whether the invoking “String” object starts with the contents of ‘str’.

      boolean endsWith(String str); Determine whether the invoking string object ends with the contents of ‘str’.

        import java.lang.*;
        class App37
        {
        public static void main(String yck[])
        {
        String x=new String("notepad");
        String y=new String("note");
        String z=new String("pad");
        if(x.startsWith(y))
        System.out.println("Yes");
        else
        System.out.println("No");
        if(x.endsWith(z))
        System.out.println("Yes");
        else
        System.out.println("No");
        }
        }
        
        
        output:
        
        \>javac App37.java
        \>java App37
        Yes
        Yes

        int compareTo(String str); Compare the contents of the invoking ‘String’ object with ‘str’.
        Return values:

        0 if both Strings are equal.<0 if length of first string is less than that of second string.>0 if length of first string is greater than that of second string.

        import java.lang.*;
        class App38
        {
        public static void main(String yck[])
        {
        String x=new String("notepad");
        String y=new String("notepad");
        if(x.compareTo(y)==0)
        System.out.println("The two strings are equal");
        else
        System.out.println("The two strings are not equal");
        }
        }
        
        
        output:
        
        \>javac App38.java
        \>java App38
        The two strings are equal
        

        int indexOf(char ch); Returns the first occurrence of ‘ch’ in the invoking String object.

        int lastIndexOf(char ch); Returns the last occurrence of ‘ch’ is the involving String objects.

          import java.lang.*;
          class App39
          {
          public static void main(String yck[])
          {
          String x=new String("notepad");
          System.out.println(x.indexOf('o'));
          System.out.println(x.lastIndexOf('p'));
          }
          }
          
          
          output:
          
          \>javac App39.java
          \>java App39
          1
          4

          String subString(int startindex); Extracts a sub string from a string.

          String subString(int startindex, int numchar); Extracts a specified number of characters from the invoking string object.

            import java.lang.*;
            class App40
            {
            public static void main(String yck[])
            {
            String s=new String("foxpro");
            String s1=s.substring(3);
            String s2=s.substring(0,3);
            System.out.println(s1);
            System.out.println(s2);
            }
            }
            
            
            output:
            
            \>javac App40.java
            \>java App40
            pro
            fox
            
            String concat(String str); Concatenates the contents of the object "str" with that of the invoking object.
            import java.lang.*;
            class App41
            {
            public static void main(String yck[])
            {
            String x=new String("Oracle");
            String y=new String(" is a date base");
            String z=x.concat(y);
            System.out.println(z);
            }
            }
            
            
            output:
            
            \>javac App41.java
            \>java App41
            Oracle is a date base

            Leave a Reply

            Your email address will not be published. Required fields are marked *