Untitled
unknown
plain_text
5 months ago
1.9 kB
2
Indexable
public class StringMethodsDemo { public static void main(String[] args) { String str = "Hello, World!"; // 1. Length System.out.println("Length: " + str.length()); if(str.charAt(0)=='H'){ str.setCharAt(0, 'p'); }; // 2. charAt System.out.println("Character at index 0: " + str.charAt(0)); // 3. Substring System.out.println("Substring from index 7: " + str.substring(7)); System.out.println("Substring from index 0 to 5: " + str.substring(0, 5)); // 4. Concatenation System.out.println("Concatenated string: " + str.concat(" How are you?")); // 5. toUpperCase and toLowerCase System.out.println("Uppercase: " + str.toUpperCase()); System.out.println("Lowercase: " + str.toLowerCase()); // 6. indexOf and lastIndexOf System.out.println("Index of 'o': " + str.indexOf('o')); System.out.println("Last index of 'o': " + str.lastIndexOf('o')); // 7. replace System.out.println("Replacing 'l' with 'L': " + str.replace('l', 'L')); // 8. startsWith and endsWith System.out.println("Starts with 'Hello': " + str.startsWith("Hello")); System.out.println("Ends with 'you?': " + str.endsWith("you?")); // 9. trim String strWithSpaces = " Hello, World! "; System.out.println("Trimmed string: " + strWithSpaces.trim()); // 10. split String[] words = str.split(", "); System.out.println("Split string:"); for (String word : words) { System.out.println(word); } // 11. equals and equalsIgnoreCase System.out.println("Equals 'Hello, World!': " + str.equals("Hello, World!")); System.out.println("EqualsIgnoreCase 'hello, world!': " + str.equalsIgnoreCase("hello, world!")); } }
Editor is loading...
Leave a Comment