Page 308 - ComputerScience_Class_11
P. 308

The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java

                 Options
                We are studying in class 11 ends with 11




              The startsWith(String str) Function
              This function checks whether the string in the current object starts with the string given as parameter. This function
              returns a Boolean type value. The syntax is:

                  boolean <variable> = String_datatype_Variable.startsWith(String str);
              Let us see the below example:


                Program 21     Write a program to check if a string starts with a specified prefix and print the result.

                1       class startswith

                2       {
                3       public static void main(String[] args)

                4       {
                5       String str1="0", str2="0";

                6       String sen1= "We are studying in class 11";
                7       String sen2= "we";

                8       boolean b=str1.startsWith(str2);
                9       if(b)
                10      System.out.println(sen1+ " begins with " + sen2);

                11      else
                12      System.out.println(sen1+ " does not begin with " + sen2);

                13      }
                14      }


              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java

                 Options
                We are studying in class 11 begins with we




              The trim() Function
              This function 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();


                  306  Touchpad Computer Science (Ver. 3.0)-XI
   303   304   305   306   307   308   309   310   311   312   313