Ad Code

Responsive Advertisement

STRINGS

 

STRINGS



A single letter can be stored in ‘char’ data type whereas a word or a set of letters are stored in ‘string’ data type within double quotes. In two methods, a string can be printed.

        System.out.println("xoxo waves");

 The text within the double quotes will be printed in the output. Here the text is given directly within the print command. The string can also be printed by assigning the text to the variable and printing the variable.

        String blog_name="xoxo waves";
        System.out.println(blog_name);

 Several operations like concatenation, trimming, starts with, ends with operation can be used with strings.

Concatenation

        String blog_name="xoxo waves + blog";
        System.out.println(blog_name);

 By using addition ‘+’ operator, two strings are added and printed in the output.

Length Calculation

        System.out.println(blog_name.length());

 By using the above command, the length of the string along with the spaces is calculated.

EndsWith command

        System.out.println(blog_name.endsWith("g"));

 The endsWith command checks whether the string ends with the character mentioned within the command. Here ‘g’ is used and since the blog name ends with ‘g’, true is printed in the output. Suppose if some other letter is used within the endsWith command, false will be printed.

StartsWith command

        System.out.println(blog_name.startsWith("o"));

 The ‘startsWith’ command checks whether the string starts with the character mentioned within the command. Since the blog name (xoxo waves) starts with ‘x’, here the command checks whether the first letter is ‘o’. And so, false is printed in the output.

isEmpty command

        System.out.println(word_1.isEmpty());

 This command checks whether the word_1 string is empty or not. Boolean outputs are printed and whenever the string is empty, true is printed else false is printed. Note that even space is taken into account.

toUppercase command

        System.out.println(word_1.toUpperCase());

 This command converts the entire string into uppercase letters whereas ‘toLowerCase’ command converts the input string to lower case letters.

Trim command

This command trims out the extra spaces at the beginning and the end of the string. The spaces present in the middle of two words are not removed.

        import java.util.Locale;
            class reference{
            public static void main(String args[]){
            String word_1="xoxo waves ";
            System.out.println(word_1);
            System.out.println(word_1.trim());
                }
            }

 ‘import java.util.Locale’ is the library function used here. The difference can be clearly seen when the output is printed using trim and without the trim operator.

Output using trim operator


Java program using strings

There are still many commands which can be done using strings and these are some basic commands which can be used for mini and bigger projects.

        import java.util.Locale;
            class reference{
            public static void main(String args[]){
            //printing a string directly
            System.out.println("Welcome to");
            //assigning a string to a variable and then printing it
            String blog_name="xoxo waves";
            System.out.println(blog_name);
            //adding 2 strings
            System.out.println("Welcome to " + blog_name);
            //Calculating length of the string
            System.out.println(blog_name.length());
            //Checking whether the string ends with 's'
            System.out.println(blog_name.endsWith("s"));
            //Checking whether the string starts with 'x'
            System.out.println(blog_name.startsWith("x"));
            //Checks whether the string is empty
            System.out.println(blog_name.isEmpty());
            //Converting the string to uppercase
            System.out.println(blog_name.toUpperCase());
            String word_1=" xoxo waves ";
            //Removing front and back spaces in a string
            String word="    Thank you    ";
            System.out.println(word);
            System.out.println(word.trim());
                }
            }

 

Output


Online compiler execution

The same code is executed in an online compiler and the same results were obtained successfully. Paste the same code and click execute to check the results. In case of error, add the keyword ‘public’ before ‘class’. 

ONLINE COMPILER



 PREVIOUS

                                                                                                        NEXT

 

Post a Comment

0 Comments