Definition and Usage
Definition and Usage
System.out.println(myStr1.equalsIgnoreCase(myStr2)); // true
System.out.println(myStr1.equalsIgnoreCase(myStr3)); // false
1. new String[n]
Purpose: This creates a new array of String objects with a specified size n.
Usage: It is used when you want to allocate memory for an array that can hold n strings,
but it does not initialize the contents of the array. All elements in the array will be null
until you assign values to them.
Example:
java
Copy code
int n = 4;
String[] myArray = new String[n]; // Creates an array to hold 4 strings
// myArray[0], myArray[1], myArray[2], myArray[3] are all null initially
2. input.split(":")
Purpose: This method splits a string into an array of substrings based on a specified
delimiter (in this case, the colon :).
Usage: It returns an array of strings that contains the segments of the original string that
are separated by the specified delimiter. The resulting array's size is determined by the
number of substrings obtained after the split.
Example:
java
Copy code
String input = "qq:ww:ee:rr";
String[] parts = input.split(":"); // Splits the string into an array
// parts will be {"qq", "ww", "ee", "rr"}
java
Copy code
String str = "Hello";
char[] charArray = str.toCharArray();
for (char c : charArray) {
System.out.println(c);
}
Explanation:
java
Copy code
String str = "Hello";
char[] chars = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']
2. charAt(int index):
java
Copy code
String str = "Hello";
char ch = str.charAt(1); // 'e'
3. String.valueOf(char[]):
java
Copy code
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.valueOf(charArray); // "Hello"
4. split(String regex):
java
Copy code
String str = "a:b:c";
String[] parts = str.split(":"); // {"a", "b", "c"}
5. substring(int beginIndex, int endIndex):
java
Copy code
String str = "Hello World";
String subStr = str.substring(0, 5); // "Hello"