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

String Class Java With Substring

Interview

Uploaded by

sristi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

String Class Java With Substring

Interview

Uploaded by

sristi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

String Class Methods in Java

String Class Methods in Java

In Java, the String class is marked as final, meaning it cannot be subclassed. Below are some

commonly used methods of the String class, and an example of how to call them:

Common Methods in the String Class:

1. char charAt(int index): Returns the character at the specified index.

2. int length(): Returns the length of the string.

3. boolean equals(Object obj): Compares this string to another object.

4. boolean equalsIgnoreCase(String anotherString): Compares strings ignoring case.

5. String substring(int beginIndex): Returns a substring starting from beginIndex.

6. String substring(int beginIndex, int endIndex): Returns a substring starting from beginIndex and

ending at endIndex - 1.

7. int indexOf(String str): Returns the index of the first occurrence of the specified string.

8. boolean contains(CharSequence s): Returns true if the string contains the specified sequence.

9. String toLowerCase(): Converts all characters to lowercase.

10. String toUpperCase(): Converts all characters to uppercase.

11. String trim(): Removes leading and trailing whitespaces.

12. boolean isEmpty(): Returns true if the string has a length of 0.

13. String replace(CharSequence target, CharSequence replacement): Replaces all occurrences of

target with replacement.

14. String[] split(String regex): Splits the string using the given regular expression.

Example:
public class Main {

public static void main(String[] args) {

// Create a string object

String str = "Hello, World!";

// Accessing methods of the final String class

System.out.println("Character at index 1: " + str.charAt(1)); // 'e'

System.out.println("Length of string: " + str.length()); // 13

System.out.println("Substring from index 7: " + str.substring(7)); // "World!"

System.out.println("Does the string contain 'World': " + str.contains("World")); // true

System.out.println("Convert to lowercase: " + str.toLowerCase()); // "hello, world!"

System.out.println("Convert to uppercase: " + str.toUpperCase()); // "HELLO, WORLD!"

System.out.println("Trimmed string: " + str.trim()); // "Hello, World!"

System.out.println("Replace 'World' with 'Java': " + str.replace("World", "Java")); // "Hello,

Java!"

}
String Class Methods in Java: substring()

In Java, the substring() method is part of the String class and is used to extract a portion of a string.

Two Variants of substring() Method:

1. String substring(int beginIndex):

- This method returns a new string that starts from the specified beginIndex and goes to the end of

the original string.

- Parameters:

- beginIndex: the starting index (inclusive).

- Returns: A new substring starting from the specified index to the end.

- Throws: IndexOutOfBoundsException if beginIndex is negative or larger than the length of the

string.

Example:

String str = "Hello, World!";

String result = str.substring(7); // Output: "World!"

2. String substring(int beginIndex, int endIndex):

- This method returns a new string that starts from the specified beginIndex and ends just before

the specified endIndex.

- Parameters:

- beginIndex: the starting index (inclusive).

- endIndex: the ending index (exclusive).

- Returns: A new substring starting from beginIndex and ending at endIndex - 1.

- Throws:
- IndexOutOfBoundsException if beginIndex or endIndex are negative, if beginIndex is greater

than endIndex, or if endIndex is larger than the length of the string.

Example:

String str = "Hello, World!";

String result = str.substring(7, 12); // Output: "World"

Important Notes:

- Zero-based Indexing: String indices in Java start at 0. For example, in "Hello", 'H' is at index 0, 'e'

is at 1, and so on.

- Immutability: Strings are immutable, so the substring() method creates a new string rather than

modifying the original string.

- Efficiency: Internally, for performance reasons, the substring shares the character array of the

original string until Java 6. From Java 7 onward, a new character array is created to avoid potential

memory leaks.

Example Program:

public class Main {

public static void main(String[] args) {

String str = "Hello, World!";

// Substring from index 7 to the end

String sub1 = str.substring(7);

System.out.println("Substring (7 to end): " + sub1); // Output: "World!"

// Substring from index 7 to 12


String sub2 = str.substring(7, 12);

System.out.println("Substring (7 to 12): " + sub2); // Output: "World"

Summary:

- substring(int beginIndex): Returns a substring from the beginIndex to the end of the string.

- substring(int beginIndex, int endIndex): Returns a substring from the beginIndex to endIndex - 1.

Both methods are used to extract parts of a string based on the given indices.

You might also like