Page 251 - Computer science 868 Class 12
P. 251

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.
                        String st1= "  Computer Science             ";
                        String st2= st1.trim();
                        System.out.println(st2);
                   Output:
                    Computer Science



                  Program 4      Write a program to input a word and create a new word after removing duplicates from the
                                 given string:
                                 For example,
                                 Input :  Applications
                                 Output:  Aplictons

                   1      import java.util.*;

                   2      class program
                   3      {

                   4          public static String remove(String s)
                   5          {
                   6              int l,c,i,j;

                   7              String org=s,  s1="";

                   8              l=s.length();
                   9              for( i=0;i<(l-1);i++)
                  10              {

                  11                  s1=s.substring(0,i+1);
                  12                  c=0;

                  13                  for(j=i+1; j<l;j++)
                  14                  {

                  15                      if(s.charAt(i)==s.charAt(j))
                  16                      {

                  17                          c++;
                  18                          continue;

                  19                      }
                  20                      else

                  21                          s1=s1+s.charAt(j);
                  22                  }

                  23                  s=s1;




                                                                                                                       249
                                                                                                            Methods    249
   246   247   248   249   250   251   252   253   254   255   256