Page 258 - IT-802_class_12
P. 258

System.out.println(“The converted string is: “ +changestr);
        Output:

        The converted string is: KOLKATA – 700114
        The String concat(String str) Function
        This function joins two strings together. The string of the current String variable whose method is called and the string
        mentioned as the parameter are the two strings that are joined. This function also returns a string. The syntax is:

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

          String str1= “india-”;
          String str2= “New Delhi”;
          String together= str1.concat(str2);

          System.out.println(“The joined string is: “ +together);
        Output:

        The joined string is: India-New Delhi
                     Notes

                    We can also use the “+” operator to concatenate two strings. For example:
                    String str1= “india-”; String str2= “New Delhi”; String together=
                    str1 + str2; System.out.println(“The joined string is:” +together);

        Output:

        The joined string is: India-New Delhi
        The String 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:
          String str= “india - New Delhi”;

          String seperate= str.substring(8);
          System.out.println(“The Extracted string is: “ +seperate);
        Output:
        The Extracted string is: New Delhi
        The boolean equals (String str) method
        This method checks whether the string of the current string object is the same as the string str in the parameter. It
        returns true, if both the strings are equal, else it returns false. This function is case-sensitive. The syntax is:

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

          String word1= “School”;
          String word2= “SChool”;
          boolean eq= word1.equals(word2);
          if(eq)

          256   Touchpad Information Technology-XII
   253   254   255   256   257   258   259   260   261   262   263