Page 424 - CA_Blue( J )_Class10
P. 424

String str2= "try";
                  boolean b=str1.endsWith(str2);
                  if(b)
                      System.out.println(str1+ " ends with " + str2);
                  else
                      System.out.println(str1+ " does not end with " + str2);
              You will get the following output:

              India is my country ends with try

              16.3.16 The trim() Method
              The trim() method is used to delete the leading and trailing spaces from the current String object. If no space is there,
              then there will be no change. It does not affect the spaces between the words. This method returns a String value. The
              syntax to use the trim() method is:

                  String <variable> = String_Object.trim();
              For example:

                  String str1= "  Computer Applications             ";
                  String str2= str.trim();
                  System.out.println(str2);
              You will get the following output:

              Computer Applications

              16.3.17 The valueOf() Method
              The valueOf() method is used to convert any primitive value to string. It is the static method of the String class. So, it is
              accessed by using the name of the String class directly. No need to create the object of the String class. The syntax to
              use the valueOf() method is:

                  String <variable> = String.valueOf(<primitive type value>);
              For example:

                  int a = 12;
                  float b = 33.42f;
                  char c = 'H';
                  double d = 123.2112;
                  System.out.println(String.valueOf(a));
                  System.out.println(String.valueOf(b));
                  System.out.println(String.valueOf(c));
                  System.out.println(String.valueOf(d));
              You will get the following output:
              12
              33.42
              H
              123.2112
              All the wrapper classes also have valueOf() method. This method takes a string value as parameter and converts it into
              respective class’s primitive data type. It is a static method. Hence, accessed by using the wrapper classes directly. The
              syntax to use the valueOf() method is:
                  <primitive type> <variable> = <Wrapper Class>.valueOf(String str);
              For example:
                  String str1 = "111";
                  int n=Integer.valueOf(str1);  // converts "111" to 111.
                  String str1 = "45.24";
                  double d=Double.valueOf(str1); // converts "45.24" to 45.24.

                422422  Touchpad Computer Applications-X
   419   420   421   422   423   424   425   426   427   428   429