Page 300 - ComputerScience_Class_11
P. 300

6       String changestr= str.toUpperCase();
                7       System.out.println("The converted string is: " +changestr);
                8       }

                9       }

              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                The converted string is: KOLKATA – 700114


              The 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);

                Program 9      Write a program to concatenate the strings and display the joined result.

                1       class string_concat

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

                5       String str1= "India-";

                6       String str2= "New Delhi";
                7       String together= str1.concat(str2);
                8       System.out.println("The joined string is: " +together);

                9       }
                10      }

              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java

                 Options

                The joined string is: India-New Delhi

                      Note: 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




                  298  Touchpad Computer Science (Ver. 3.0)-XI
   295   296   297   298   299   300   301   302   303   304   305