0% found this document useful (0 votes)
5 views

Definition and Usage

The document explains various string manipulation methods in Java, including equalsIgnoreCase(), toCharArray(), charAt(), and split(). It provides examples for each method, demonstrating their purpose and usage. Additionally, it covers how to create a new string array and extract substrings from a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Definition and Usage

The document explains various string manipulation methods in Java, including equalsIgnoreCase(), toCharArray(), charAt(), and split(). It provides examples for each method, demonstrating their purpose and usage. Additionally, it covers how to create a new string array and extract substrings from a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Definition and Usage

The equalsIgnoreCase() method compares two strings, ignoring lower case


and upper case differences.

String myStr1 = "Hello";

String myStr2 = "HELLO";

String myStr3 = "Another String";

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:

 toCharArray(): This method converts the string into an array of characters.


 char[] charArray: The resulting character array will contain each character of the string as
individual elements.

Output for the example above:


Copy code
H
e
l
l
o
1. toCharArray():

 Converts a String into a char[] array.


 Usage: When you need to work with individual characters of a string.
 Example:

java
Copy code
String str = "Hello";
char[] chars = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']
2. charAt(int index):

 Retrieves the character at a specific index in the string.


 Usage: When you need to access a single character from a string.
 Example:

java
Copy code
String str = "Hello";
char ch = str.charAt(1); // 'e'
3. String.valueOf(char[]):

 Converts a char[] array into a String.


 Usage: When you want to convert a character array back to a string.
 Example:

java
Copy code
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.valueOf(charArray); // "Hello"
4. split(String regex):

 Splits a string into an array of substrings based on a specified delimiter.


 Usage: When you need to break a string into parts (e.g., tokens).
 Example:

java
Copy code
String str = "a:b:c";
String[] parts = str.split(":"); // {"a", "b", "c"}
5. substring(int beginIndex, int endIndex):

 Extracts a substring from the string.


 Usage: When you need to extract a portion of a string.
 Example:

java
Copy code
String str = "Hello World";
String subStr = str.substring(0, 5); // "Hello"

You might also like