23 Jan Java Prog 1

 

Class Declaration

  1. public class StringOperations {
    • Defines a public class named StringOperations.

Main Method

  1. public static void main(String[] args) {
    • Entry point of the program.

1. Creating and Initializing Strings

  1. String str1 = "Hello";
    • Declares and initializes a string str1 with the value "Hello".
  2. String str2 = "World";
    • Declares and initializes another string str2 with "World".
  3. String str3 = "Hello";
    • Declares a string str3 with the same value as str1 for comparison examples.

2. Concatenation

  1. String concatenated = str1 + " " + str2;
    • Concatenates str1, a space, and str2, storing the result in concatenated.
  2. System.out.println("Concatenated String: " + concatenated);
    • Prints the concatenated string.

3. Length of a String

  1. System.out.println("Length of str1: " + str1.length());
    • Prints the length of str1 using the length() method.

4. Character Extraction

  1. System.out.println("Character at index 1 in str1: " + str1.charAt(1));
    • Prints the character at index 1 in str1 using charAt().

5. Substring Extraction

  1. System.out.println("Substring of str2 (1 to 4): " + str2.substring(1, 4));
    • Extracts and prints a substring of str2 from index 1 (inclusive) to 4 (exclusive).

6. Comparison

  1. System.out.println("str1 equals str3: " + str1.equals(str3));
    • Compares str1 and str3 for equality using equals() and prints the result.
  2. System.out.println("str1 equals str2: " + str1.equals(str2));
    • Compares str1 and str2 for equality.
  3. System.out.println("str1 compareTo str2: " + str1.compareTo(str2));
    • Performs lexicographical comparison using compareTo() and prints the result.

7. Case Conversion

  1. System.out.println("str1 in uppercase: " + str1.toUpperCase());
    • Converts str1 to uppercase and prints it.
  2. System.out.println("str2 in lowercase: " + str2.toLowerCase());
    • Converts str2 to lowercase and prints it.

8. Searching in Strings

  1. System.out.println("Index of 'l' in str1: " + str1.indexOf('l'));
    • Finds and prints the first occurrence index of 'l' in str1.
  2. System.out.println("Last index of 'l' in str1: " + str1.lastIndexOf('l'));
    • Finds and prints the last occurrence index of 'l'.
  3. System.out.println("str1 contains 'He': " + str1.contains("He"));
    • Checks if str1 contains the substring "He".

9. Replacing Characters or Substrings

  1. String replacedString = str1.replace('l', 'z');
    • Replaces all occurrences of 'l' with 'z' in str1.
  2. System.out.println("Replaced 'l' with 'z' in str1: " + replacedString);
    • Prints the modified string.

10. String Splitting

  1. String sentence = "Java is fun to learn";
    • Initializes a string sentence.
  2. String[] words = sentence.split(" ");
    • Splits sentence into an array of words.
  3. for (String word : words) {
    • Iterates through the array words and prints each word.

11. Trimming Whitespace

  1. String strWithSpaces = " Hello World ";
    • Initializes a string with leading and trailing spaces.
  2. System.out.println("String after trimming: '" + strWithSpaces.trim() + "'");
    • Removes the spaces using trim() and prints the result.

12. Converting Other Types to Strings

  1. int number = 123;
    • Declares an integer variable.
  2. String numberStr = String.valueOf(number);
    • Converts number to a string using String.valueOf().
  3. System.out.println("Converted number to string: " + numberStr);
    • Prints the converted string.

13. StringBuilder and StringBuffer (Mutable Strings)

  1. StringBuilder sb = new StringBuilder("Hello");
    • Creates a mutable StringBuilder object with "Hello".
  2. sb.append(" World");
    • Appends " World" to sb.
  3. System.out.println("StringBuilder result: " + sb.toString());
    • Prints the updated string.
  4. StringBuffer sbf = new StringBuffer("Java");
    • Creates a mutable StringBuffer object with "Java".
  5. sbf.insert(4, " Programming");
    • Inserts " Programming" at index 4 in sbf.
  6. System.out.println("StringBuffer result: " + sbf.toString());
    • Prints the updated string.

14. String Immutability Demonstration

  1. String immutableStr = "Immutable";
    • Declares a string.
  2. String newStr = immutableStr.concat(" String");
    • Concatenates " String" to immutableStr (does not modify immutableStr).
  3. System.out.println("Original String: " + immutableStr);
    • Prints the original string.
  4. System.out.println("Modified String: " + newStr);
    • Prints the new concatenated string.

15. Joining Strings

  1. String joined = String.join(", ", "Apple", "Banana", "Cherry");
    • Joins strings with a separator ", ".
  2. System.out.println("Joined String: " + joined);
    • Prints the joined string.

16. Checking if String is Empty or Blank

  1. String emptyStr = "";
    • Declares an empty string.
  2. String blankStr = " ";
    • Declares a blank string (spaces only).
  3. System.out.println("Is emptyStr empty? " + emptyStr.isEmpty());
    • Checks if emptyStr is empty using isEmpty().
  4. System.out.println("Is blankStr blank? " + blankStr.isBlank());
    • Checks if blankStr is blank using isBlank().

17. Reversing a String

  1. String reverse = new StringBuilder(str1).reverse().toString();
    • Reverses str1 using StringBuilder and prints the result.

18. Unicode and Code Points

  1. System.out.println("Code point at index 0 in str1: " + str1.codePointAt(0));
    • Prints the Unicode code point of the character at index 0 in str1.

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe