23 Jan Java Prog 1
Class Declaration
public class StringOperations {
- Defines a public class named
StringOperations
.
- Defines a public class named
Main Method
public static void main(String[] args) {
- Entry point of the program.
1. Creating and Initializing Strings
String str1 = "Hello";
- Declares and initializes a string
str1
with the value"Hello"
.
- Declares and initializes a string
String str2 = "World";
- Declares and initializes another string
str2
with"World"
.
- Declares and initializes another string
String str3 = "Hello";
- Declares a string
str3
with the same value asstr1
for comparison examples.
- Declares a string
2. Concatenation
String concatenated = str1 + " " + str2;
- Concatenates
str1
, a space, andstr2
, storing the result inconcatenated
.
- Concatenates
System.out.println("Concatenated String: " + concatenated);
- Prints the concatenated string.
3. Length of a String
System.out.println("Length of str1: " + str1.length());
- Prints the length of
str1
using thelength()
method.
- Prints the length of
4. Character Extraction
System.out.println("Character at index 1 in str1: " + str1.charAt(1));
- Prints the character at index 1 in
str1
usingcharAt()
.
- Prints the character at index 1 in
5. Substring Extraction
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).
- Extracts and prints a substring of
6. Comparison
System.out.println("str1 equals str3: " + str1.equals(str3));
- Compares
str1
andstr3
for equality usingequals()
and prints the result.
- Compares
System.out.println("str1 equals str2: " + str1.equals(str2));
- Compares
str1
andstr2
for equality.
- Compares
System.out.println("str1 compareTo str2: " + str1.compareTo(str2));
- Performs lexicographical comparison using
compareTo()
and prints the result.
- Performs lexicographical comparison using
7. Case Conversion
System.out.println("str1 in uppercase: " + str1.toUpperCase());
- Converts
str1
to uppercase and prints it.
- Converts
System.out.println("str2 in lowercase: " + str2.toLowerCase());
- Converts
str2
to lowercase and prints it.
- Converts
8. Searching in Strings
System.out.println("Index of 'l' in str1: " + str1.indexOf('l'));
- Finds and prints the first occurrence index of
'l'
instr1
.
- Finds and prints the first occurrence index of
System.out.println("Last index of 'l' in str1: " + str1.lastIndexOf('l'));
- Finds and prints the last occurrence index of
'l'
.
- Finds and prints the last occurrence index of
System.out.println("str1 contains 'He': " + str1.contains("He"));
- Checks if
str1
contains the substring"He"
.
- Checks if
9. Replacing Characters or Substrings
String replacedString = str1.replace('l', 'z');
- Replaces all occurrences of
'l'
with'z'
instr1
.
- Replaces all occurrences of
System.out.println("Replaced 'l' with 'z' in str1: " + replacedString);
- Prints the modified string.
10. String Splitting
String sentence = "Java is fun to learn";
- Initializes a string
sentence
.
- Initializes a string
String[] words = sentence.split(" ");
- Splits
sentence
into an array of words.
- Splits
for (String word : words) {
- Iterates through the array
words
and prints each word.
- Iterates through the array
11. Trimming Whitespace
String strWithSpaces = " Hello World ";
- Initializes a string with leading and trailing spaces.
System.out.println("String after trimming: '" + strWithSpaces.trim() + "'");
- Removes the spaces using
trim()
and prints the result.
- Removes the spaces using
12. Converting Other Types to Strings
int number = 123;
- Declares an integer variable.
String numberStr = String.valueOf(number);
- Converts
number
to a string usingString.valueOf()
.
- Converts
System.out.println("Converted number to string: " + numberStr);
- Prints the converted string.
13. StringBuilder and StringBuffer (Mutable Strings)
StringBuilder sb = new StringBuilder("Hello");
- Creates a mutable
StringBuilder
object with"Hello"
.
- Creates a mutable
sb.append(" World");
- Appends
" World"
tosb
.
- Appends
System.out.println("StringBuilder result: " + sb.toString());
- Prints the updated string.
StringBuffer sbf = new StringBuffer("Java");
- Creates a mutable
StringBuffer
object with"Java"
.
- Creates a mutable
sbf.insert(4, " Programming");
- Inserts
" Programming"
at index 4 insbf
.
- Inserts
System.out.println("StringBuffer result: " + sbf.toString());
- Prints the updated string.
14. String Immutability Demonstration
String immutableStr = "Immutable";
- Declares a string.
String newStr = immutableStr.concat(" String");
- Concatenates
" String"
toimmutableStr
(does not modifyimmutableStr
).
- Concatenates
System.out.println("Original String: " + immutableStr);
- Prints the original string.
System.out.println("Modified String: " + newStr);
- Prints the new concatenated string.
15. Joining Strings
String joined = String.join(", ", "Apple", "Banana", "Cherry");
- Joins strings with a separator
", "
.
- Joins strings with a separator
System.out.println("Joined String: " + joined);
- Prints the joined string.
16. Checking if String is Empty or Blank
String emptyStr = "";
- Declares an empty string.
String blankStr = " ";
- Declares a blank string (spaces only).
System.out.println("Is emptyStr empty? " + emptyStr.isEmpty());
- Checks if
emptyStr
is empty usingisEmpty()
.
- Checks if
System.out.println("Is blankStr blank? " + blankStr.isBlank());
- Checks if
blankStr
is blank usingisBlank()
.
- Checks if
17. Reversing a String
String reverse = new StringBuilder(str1).reverse().toString();
- Reverses
str1
usingStringBuilder
and prints the result.
- Reverses
18. Unicode and Code Points
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
.
- Prints the Unicode code point of the character at index 0 in
Comments
Post a Comment