Page 80 - CTS - CSA TP - Volume 2
P. 80
COMPUTER SOFTWARE APPLICATION - CITS
TASK 3: Java program to perform various string methods in single program
public class StringMethodsExample {
public static void main(String[] args) {
// Example String
String originalString = “Hello, World!”;
System.out.println(“Original String: “ + originalString);
// 1. Convert to Uppercase
String uppercaseString = originalString.toUpperCase();
System.out.println(“1. Uppercase: “ + uppercaseString);
// 2. Convert to Lowercase
String lowercaseString = originalString.toLowerCase();
System.out.println(“2. Lowercase: “ + lowercaseString);
// 3. Substring
String substring = originalString.substring(7, 12);
System.out.println(“3. Substring ( Display ‘World’ from the Original String): “ + substring);
// 4. Index of a Character
int indexOfW = originalString.indexOf(‘W’);
System.out.println(“4. Index of ‘W’ (index value start from 0): “ + indexOfW);
// 5. Replace characters
String replacedString = originalString.replace(‘o’, ‘X’);
System.out.println(“5. Replaced String ( Replacing ‘o’ with ‘X’): “ + replacedString);
// 6. Check if starts with “Hello”
boolean startsWithHello = originalString.startsWith(“Hello”);
System.out.println(“6. Starts with ‘Hello’: “ + startsWithHello);
// 7. Check if ends with “World!”
boolean endsWithWorld = originalString.endsWith(“World!”);
System.out.println(“7. Ends with ‘World!’: “ + endsWithWorld);
// 8. Trim leading and trailing whitespaces
String stringWithWhitespaces = “ Trim Me “;
65
CITS : IT & ITES - Computer Software Application - Exercise 92