0% found this document useful (0 votes)
11 views3 pages

Essential Java String Methods Guide

The document provides an overview of Java String methods, including creation, comparison, substring manipulation, searching, splitting, and joining strings. It also covers converting data to strings and using mutable strings with StringBuilder and StringBuffer. Each section includes method descriptions and code examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Essential Java String Methods Guide

The document provides an overview of Java String methods, including creation, comparison, substring manipulation, searching, splitting, and joining strings. It also covers converting data to strings and using mutable strings with StringBuilder and StringBuffer. Each section includes method descriptions and code examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java String Methods

### 1. String Creation & Basic Methods


- `length()`: Returns the length of the string.
- `isEmpty()`: Checks if the string is empty.
- `charAt(index)`: Returns the character at the specified index.
- `toLowerCase()`: Converts the string to lowercase.
- `toUpperCase()`: Converts the string to uppercase.
- `trim()`: Removes leading and trailing spaces.

Example:
```java
String str = " Hello ";
[Link]([Link]()); // 7
[Link]([Link]()); // "Hello"
[Link]([Link]()); // " HELLO "
```

### 2. String Comparison


- `equals(str2)`: Checks if two strings are equal (case-sensitive).
- `equalsIgnoreCase(str2)`: Checks equality, ignoring case
differences.
- `compareTo(str2)`: Compares two strings lexicographically.
- `compareToIgnoreCase(str2)`: Compares two strings, ignoring case.
- `contains(substring)`: Checks if a string contains a specified
substring.
- `startsWith(prefix)`: Checks if a string starts with a given prefix.
- `endsWith(suffix)`: Checks if a string ends with a given suffix.

Example:
```java
String s1 = "Java";
String s2 = "java";
[Link]([Link](s2)); // false
[Link]([Link](s2)); // true
[Link]([Link]("Ja")); // true
[Link]([Link]("va")); // true
```

### 3. Substring & Modification


- `substring(beginIndex)`: Returns substring from `beginIndex` to end.
- `substring(beginIndex, endIndex)`: Returns substring within the
range.
- `replace(oldChar, newChar)`: Replaces characters in a string.
- `replaceAll(regex, replacement)`: Replaces all matches of a regex.

Example:
```java
String s = "Java Programming";
[Link]([Link](5)); // "Programming"
[Link]([Link]('a', '@')); // "J@v@ Progr@mming"
```

### 4. Searching in Strings


- `indexOf(char/str)`: Returns the first occurrence index.
- `lastIndexOf(char/str)`: Returns the last occurrence index.

Example:
```java
String s = "Hello World";
[Link]([Link]('o')); // 4
[Link]([Link]('o')); // 7
```

### 5. Splitting & Joining Strings


- `split(regex)`: Splits a string into an array.
- `join(delimiter, elements...)`: Joins multiple strings.

Example:
```java
String s = "apple,banana,grape";
String[] fruits = [Link](",");
[Link]([Link](fruits)); // [apple, banana, grape]
```
### 6. Converting Data to String
- `valueOf(data)`: Converts primitive types to a string.
- `toString()`: Converts an object to a string.

Example:
```java
int num = 100;
String str = [Link](num);
[Link](str); // "100"
```

### 7. StringBuilder & StringBuffer (Mutable Strings)


- `append(str)`: Appends a string.
- `insert(index, str)`: Inserts a string.
- `delete(start, end)`: Deletes characters.
- `reverse()`: Reverses the string.

Example:
```java
StringBuilder sb = new StringBuilder("Hello");
[Link](" World");
[Link](sb); // "Hello World"
[Link]();
[Link](sb); // "dlroW olleH"
```

These are the most commonly used `String` methods in Java.

You might also like