# String Methods

`String` is a fundamental class in Java for working with text. It provides a wide range of methods for manipulating and inspecting strings. This documentation covers some of the most frequently used methods with real-time examples.

### 1. `length()`

#### Method Signature

```java
int length()
```

#### Description

This method returns the length (the number of characters) of the `String`.

#### Example

```java
String text = "Hello, World!";
int length = text.length(); // length will be 13
```

### 2. `charAt(int index)`

#### Method Signature

```java
char charAt(int index)
```

#### Description

This method returns the character at the specified index in the `String`.

#### Example

```java
String text = "Java";
char character = text.charAt(1); // character will be 'a'
```

### 3. `substring(int beginIndex)`

#### Method Signature

```java
String substring(int beginIndex)
```

#### Description

This method returns a new `String` that is a substring of the original string starting from the specified `beginIndex`.

#### Example

```java
String text = "Hello, World!";
String substring = text.substring(7); // substring will be "World!"
```

### 4. `substring(int beginIndex, int endIndex)`

#### Method Signature

```java
String substring(int beginIndex, int endIndex)
```

#### Description

This method returns a new `String` that is a substring of the original string, starting from `beginIndex` (inclusive) and ending at `endIndex` (exclusive).

#### Example

```java
String text = "Hello, World!";
String substring = text.substring(7, 12); // substring will be "World"
```

### 5. `concat(String str)`

#### Method Signature

```java
String concat(String str)
```

#### Description

This method concatenates the specified string (`str`) to the end of the original string and returns the resulting `String`.

#### Example

```java
String text = "Hello";
String newText = text.concat(", World!"); // newText will be "Hello, World!"
```

### 6. `equals(Object obj)`

#### Method Signature

```java
boolean equals(Object obj)
```

#### Description

This method compares the content of two strings and returns `true` if they are equal, `false` otherwise.

#### Example

```java
String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2); // isEqual will be true
```

### 7. `equalsIgnoreCase(String anotherString)`

#### Method Signature

```java
boolean equalsIgnoreCase(String anotherString)
```

#### Description

This method compares two strings, ignoring their case (uppercase/lowercase), and returns `true` if they are equal, `false` otherwise.

#### Example

```java
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // isEqual will be true
```

### 8. `startsWith(String prefix)`

#### Method Signature

```java
boolean startsWith(String prefix)
```

#### Description

This method checks if the `String` starts with the specified prefix and returns `true` if it does, `false` otherwise.

#### Example

```java
String text = "Hello, World!";
boolean startsWithHello = text.startsWith("Hello"); // startsWithHello will be true
```

### 9. `endsWith(String suffix)`

#### Method Signature

```java
boolean endsWith(String suffix)
```

#### Description

This method checks if the `String` ends with the specified suffix and returns `true` if it does, `false` otherwise.

#### Example

```java
String text = "Hello, World!";
boolean endsWithWorld = text.endsWith("World!"); // endsWithWorld will be true
```

### 10. `contains(CharSequence sequence)`

#### Method Signature

```java
boolean contains(CharSequence sequence)
```

#### Description

This method checks if the `String` contains the specified sequence of characters and returns `true` if it does, `false` otherwise.

#### Example

```java
String text = "The quick brown fox";
boolean containsFox = text.contains("fox"); // containsFox will be true
```

### 11. `replace(CharSequence target, CharSequence replacement)`

#### Method Signature

```java
String replace(CharSequence target, CharSequence replacement)
```

#### Description

This method replaces all occurrences of the `target` character sequence with the `replacement` character sequence and returns the resulting `String`.

#### Example

```java
String text = "I love Java. Java is great!";
String replacedText = text.replace("Java", "Python");
// replacedText will be "I love Python. Python is great!"
```

### 12. `trim()`

#### Method Signature

```java
String trim()
```

#### Description

This method removes leading and trailing whitespace from the `String` and returns the resulting `String`.

#### Example

```java
String text = "   This is a string with whitespace.   ";
String trimmedText = text.trim();
// trimmedText will be "This is a string with whitespace."
```

### 13. `split(String regex)`

#### Method Signature

```java
String[] split(String regex)
```

#### Description

This method splits the `String` into an array of substrings based on the provided regular expression (`regex`) and returns the array.

#### Example

```java
String text = "apple,banana,cherry";
String[] fruits = text.split(",");
// fruits will be ["apple", "banana", "cherry"]
```

### 14. `toUpperCase()` and `toLowerCase()`

#### Method Signature

```java
String toUpperCase()
String toLowerCase()
```

#### Description

These methods convert all characters in the `String` to uppercase or lowercase, respectively, and return the resulting `String`.

#### Example

```java
String text = "Hello, World!";
String upperCaseText = text.toUpperCase(); // upperCaseText will be "HELLO, WORLD!"
String lowerCaseText = text.toLowerCase(); // lowerCaseText will be "hello, world!"
```

### 15. `isEmpty()`

#### Method Signature

```java
boolean isEmpty()
```

#### Description

This method checks if the `String` is empty (has a length of 0) and returns `true` if it is, `false` otherwise.

#### Example

```java
String emptyString = "";
boolean isEmpty = emptyString.isEmpty(); // isEmpty will be true
```

***

These are some of the most frequently used `String` methods in Java, along with real-time examples. Strings are essential in Java for working with text data, and understanding these methods will help you manipulate and analyze text efficiently in your Java applications.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codewithmeiy.gitbook.io/core-java/data-types/string-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
