PLAYING WITH STRING IN JAVA

Well, I must say string is the must easily understandable concept in java and fun to learn and work with. Here's a sample code that I wrote and learn to perform different operations on string variable.

package Lesson1;

public class StringClass {

    public static void main(String[] args) {
        String word="Leo Umesh";
        
        
        String word_uppercase= word.toUpperCase();
        System.out.println(word_uppercase);
        
        int word_length =  word.length();
        System.out.println("String length is " + word_length);
        
        String incorrect_word="Newal";
        String correct_word= incorrect_word.replace('w', 'p');
        System.out.println("The correct word is "+ correct_word);
        
        String name1="Leo";
        String name2="Leo";
        System.out.println(name1.compareTo(name2));
    }

}


Here's the output of the above code.


Screenshot_3.png

You can also execute the above code online here. You have to copy starting from public static void main to second last curly bracket and then put those code within public class Main in the online java code compiler.



0
0
0.000
0 comments