Define String handling. Explain special string operations with examples

6.A] Define String handling. Explain special string operations with examples.

Answer:-

String handling in Java refers to the process of creating, manipulating, and managing strings. Strings in Java are objects that represent sequences of characters. The String class provides various methods and operations for handling these sequences efficiently.

Special String Operations

Java provides several special operations for handling strings, which include concatenation, comparison, searching, modifying, and more.

1. String Concatenation

  • Combines two or more strings into one.
  • Syntax:
  String result = str1 + str2;

or

  String result = str1.concat(str2);
  • Example:
  String str1 = "Hello";
  String str2 = "World";
  String result = str1 + " " + str2;
  System.out.println(result); // Output: Hello World

2. String Comparison

  • Compares two strings for equality or order.
  • Methods:
  • equals(): Checks if two strings are exactly the same.
  • equalsIgnoreCase(): Compares strings without considering case.
  • compareTo(): Compares strings lexicographically.
  • compareToIgnoreCase(): Lexicographical comparison ignoring case.
  • Example:
  String str1 = "Hello";
  String str2 = "hello";
  boolean isEqual = str1.equals(str2);
  boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
  int comparison = str1.compareTo(str2);

  System.out.println("isEqual: " + isEqual); // Output: false
  System.out.println("isEqualIgnoreCase: " + isEqualIgnoreCase); // Output: true
  System.out.println("comparison: " + comparison); // Output: negative value

3. Substring Extraction

  • Extracts a portion of a string based on the specified index.
  • Methods:
  • substring(int beginIndex): Extracts from the specified index to the end.
  • substring(int beginIndex, int endIndex): Extracts between the specified indices.
  • Example:
  String str = "HelloWorld";
  String subStr1 = str.substring(5);
  String subStr2 = str.substring(0, 5);

  System.out.println("subStr1: " + subStr1); // Output: World
  System.out.println("subStr2: " + subStr2); // Output: Hello

4. String Length

  • Returns the number of characters in a string.
  • Method:
  • length(): Returns the length of the string.
  • Example:
  String str = "HelloWorld";
  int length = str.length();
  System.out.println("Length: " + length); // Output: 10

5. String Replacement

  • Replaces characters or sequences of characters within a string.
  • Methods:
  • replace(char oldChar, char newChar): Replaces all occurrences of the old character with the new character.
  • replace(CharSequence target, CharSequence replacement): Replaces all occurrences of the target sequence with the replacement sequence.
  • Example:
  String str = "Hello World";
  String replacedStr = str.replace('o', 'a');
  String replacedStr2 = str.replace("World", "Java");

  System.out.println("replacedStr: " + replacedStr); // Output: Hella Warld
  System.out.println("replacedStr2: " + replacedStr2); // Output: Hello Java

6. String Case Conversion

  • Converts strings to upper or lower case.
  • Methods:
  • toUpperCase(): Converts all characters in the string to uppercase.
  • toLowerCase(): Converts all characters in the string to lowercase.
  • Example:
  String str = "Hello World";
  String upperCaseStr = str.toUpperCase();
  String lowerCaseStr = str.toLowerCase();

  System.out.println("upperCaseStr: " + upperCaseStr); // Output: HELLO WORLD
  System.out.println("lowerCaseStr: " + lowerCaseStr); // Output: hello world

7. String Trimming

  • Removes leading and trailing whitespace from a string.
  • Method:
  • trim(): Removes whitespace from both ends of the string.
  • Example:
  String str = "  Hello World  ";
  String trimmedStr = str.trim();
  System.out.println("trimmedStr: '" + trimmedStr + "'"); // Output: 'Hello World'

Summary

  • Concatenation: Combines strings using + or concat().
  • Comparison: Compares strings using equals(), compareTo(), etc.
  • Substring Extraction: Extracts parts of strings using substring().
  • Length: Gets the length of a string using length().
  • Replacement: Replaces characters or sequences using replace().
  • Case Conversion: Changes string case using toUpperCase() or toLowerCase().
  • Trimming: Removes whitespace using trim().

Leave a Reply

Your email address will not be published. Required fields are marked *