Page 301 - ComputerScience_Class_11
P. 301

The substring(int index) Function
                 This function  extracts all the characters from the index position till the end of the string. It returns a string, which is a
                 substring of the given string. The syntax is:
                    String <variable> = String_datatype_Variable.substring(int index);
                 Let us see the below example:

                                 Write a program to extract a substring starting from index 8 in the string and display the
                   Program 10
                                 result.
                   1      class substring

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

                   5      String str= "India - New Delhi";

                   6      String seperate= str.substring(8);
                   7      System.out.println("The Extracted string is: " +seperate);
                   8      }

                   9      }

                 The output of the preceding program is as follows:
                       BlueJ: Terminal Window - Java

                   Options

                  The Extracted string is: New Delhi


                 The substring(int startindex, int endindex) Function
                 This function extracts all the characters from the index position ‘startindex’ till 1 less than the ‘endindex’. It always
                 returns a string, which is also a substring of the original string. The syntax is:

                    String <variable> = String_datatype_Variable.substring(int startindex, int endindex);
                 Let us see the below example:

                   Program 11    Write a program to extract a substring from index 8 to 11 in the string and display the result.


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

                   4      {

                   5      String str= "India - New Delhi";
                   6      String removestr= str.substring(8,11);
                   7      System.out.println("The Extracted string is: " + removestr);

                   8      }
                   9      }





                                                                                                           Strings  299
   296   297   298   299   300   301   302   303   304   305   306