Page 260 - IT-802_class_12
P. 260

Let us see the below example:

          String str1=”0”, str2=”0”;
          String sen1= “We are studying in class 12”;

          String sen2= “we”;
          boolean b=str1.startsWith(str2);
          if(b)
          System.out.println(sen1+ “ begins with “ + sen2);

          else
          System.out.println(sen1+ “ does not begin with “ + sen2);
        The output of the preceding program is as follows:
        We are studying in class 12 begins with we
        The String trim() method

        This method is used to delete the leading and trailing spaces from the string of the current object. If there is no space,
        then there will be no change. Also, it does not affect the spaces between the words. This function returns a value of
        the String type. The syntax of the trim() method is:
          String <variable> = String_datatype_Variable.trim();
        Let us see the below example:

          String sen1= “ Computer Science “;
          System.out.println(sen1.trim());
        Output:
        Computer Science

        Example: To input a sentence, and convert the small letters to capital letters and vice versa.
        package com.mycompany.string_count;

        import java.util.*;
        class String_count
        {

          public static void main(String [] args)
          {
            Scanner sc= new Scanner(System.in);
            String str,newstr=””;

            int i,l;
            char ch,ch1;
            System.out.print(“Enter a sentence”);

           str=sc.nextLine();
           l=str.length();
           for(i=0;i<l;i++)
           {

             ch=str.charAt(i);


          258   Touchpad Information Technology-XII
   255   256   257   258   259   260   261   262   263   264   265