Page 354 - Computer science 868 Class 12
P. 354

System.out.println(str1+ " ends with " + str2 );
                      else
                      System.out.println(str1+ " does not end with " + str2);
                  Output:
                  India is my country ends with try
              18. boolean startsWith(String str): This method checks whether the string in the current object starts with the string
                  in the parameter. This function returns a data type of the boolean value.
                  Syntax: boolean  <variable> = Stringobject.startsWith(String str);
                  Example:

                      String str1 = "Study hard to ";
                      String str2 = "dia";
                      boolean b = str1.startsWith(str2);;
                      if(b)
                      System.out.println(str1+ " begins with " + str2 );
                      else
                      System.out.println(str1+ " does not begin with " + str2);
                  Output:
                  India is my country does not begin with dia
              19. String trim(): This method is used to delete the leading and trailing spaces from the string of the current object. If
                  no space is there, then there will be no change. Also, it does not affect the spaces between the words. This method
                  returns a value of String type.
                  Syntax: String <variable> = Stringobject.trim();
                  Example:

                      String str1 = "  Computer Applications             ";
                      String str2 = str.trim();
                      System.out.println(str2);
                  Output:

                  Computer Applications

                  10.3 STRING BUFFER FUNCTION
              Sometimes, it is necessary to use String Buffer functions as it creates reasonable space in the memory.
              1.  StringBuffer() creates space in memory to store a string.

                      StringBuffer sb = new StringBuffer() ;
              2.  StringBuffer() also takes parameters. For example,
                      StringBuffer sb = new StringBuffer("India") ;
                Here, the method allocates memory to contain a String variable with value “India”.
                There are many built-in methods for String methods which perform the following things.
                a.  append()     :   It is used to add a string at the last of another string.
                b.  setCharAt()    :  It is used to replace a character with another character.
                c.  insert()     :  It is used to insert a string at the specified index.
                d.  delete()     :  It is used to delete  a character for a range of index in the given string.
                e.  setLength()   :  It is used to set the length of a string buffered after a specified range.
                f.  reverse()    :  It is used to reverse the characters in the String.

              The above methods are demonstrated below in the table with example and output.




                352352  Touchpad Computer Science-XII
   349   350   351   352   353   354   355   356   357   358   359