JAVA RECORD PROGRAM TO UNDERSTAND STRING OPERATIONS

 public class StringExample {

    public static void main(String[] args) {

        // Creating a string

        String str = "Hello, World!";

        

        // Length of the string

        int length = str.length();

        System.out.println("Length of the string: " + length);

        

        // Getting character at a specific index

        char ch = str.charAt(7);

        System.out.println("Character at index 7: " + ch);

        

        // Substring

        String substr = str.substring(7, 12);

        System.out.println("Substring from index 7 to 11: " + substr);

        

        // Converting to uppercase

        String upperCase = str.toUpperCase();

        System.out.println("Uppercase string: " + upperCase);

        

        // Converting to lowercase

        String lowerCase = str.toLowerCase();

        System.out.println("Lowercase string: " + lowerCase);

        

        // Index of a substring

        int index = str.indexOf("World");

        System.out.println("Index of 'World': " + index);

        

        // Checking if starts with

        boolean startsWith = str.startsWith("Hello");

        System.out.println("Starts with 'Hello': " + startsWith);

        

        // Checking if ends with

        boolean endsWith = str.endsWith("!");

        System.out.println("Ends with '!': " + endsWith);

        

        // Splitting the string

        String[] parts = str.split(",");

        System.out.println("Splitting the string:");

        for (String part : parts) {

            System.out.println(part.trim());

        }

        

        // Replacing characters

        String replaced = str.replace("World", "Universe");

        System.out.println("After replacing 'World' with 'Universe': " + replaced);

        

        // Trimming whitespace

        String trimmed = "  Hello, World!  ".trim();

        System.out.println("After trimming whitespace: " + trimmed);

    }

}

Comments

Popular posts from this blog

PROGRAM FOR BANKERS ALGORITHM FOR DEADLOCK AVOIDENCE

JAVA RECORD PROGRAM FOR AREA AND PERIMETER OF RECTANGLE

JAVA PROGRAM TO PERFORM MULTI THREADING USING RUNNABLE INTERFACE