New String Methods From Java 11

In this post, we will see the Nlnew String Methods that are added in Java 11 with examples 

Introduction:
Java 11 introduced several new methods to the `String` class, offering enhanced functionality and flexibility for handling and manipulating strings. In this blog post, we will delve into these new methods and explore how they can be utilized to streamline your Java programming tasks. Whether you're a seasoned developer or just starting with Java, understanding these new string methods will undoubtedly benefit your coding endeavors. Let's dive in!

1. `isBlank()` - Detecting Empty or Whitespace Strings:
The `isBlank()` method checks if a string is empty or consists only of whitespace characters. It returns `true` if the string is blank; otherwise, it returns `false`. This method is particularly useful for input validation and handling user-generated content. Take a look at the example below:

```java
String str = " ";
boolean isBlank = str.isBlank();
System.out.println(isBlank); // Output: true
```

2. `lines()` - Splitting a String into Lines:
The `lines()` method splits a string into a stream of lines, based on the line terminators (`\n`, `\r`, or `\r\n`). It provides a convenient way to iterate through the lines of a string. Here's an example:

```java
String text = "Hello\nWorld\nJava 11";
text.lines().forEach(System.out::println); // Output: Hello, World, Java 11
```

3. `strip()`, `stripLeading()`, and `stripTrailing()` - Removing Whitespace:
These three methods allow you to remove leading, trailing, or both leading and trailing whitespace from a string, respectively. They are an improvement over the existing `trim()` method as they handle all Unicode whitespace characters. Here's an example:

```java
String str = " Java 11 ";
String stripped = str.strip();
String strippedLeading = str.stripLeading();
String strippedTrailing = str.stripTrailing();
System.out.println(stripped); // Output: "Java 11"
System.out.println(strippedLeading); // Output: "Java 11 "
System.out.println(strippedTrailing); // Output: " Java 11"
```

4. `repeat(int count)` - Repeating a String:
The `repeat()` method repeats a string a specified number of times and returns the concatenated result. It is a concise way to generate repeated patterns or duplicate strings. See the example below:

```java
String repeated = "Java ".repeat(3);
System.out.println(repeated); // Output: "Java Java Java "
```

5. `lines().count()` - Counting the Number of Lines in a String:
By combining the `lines()` method with the `count()` method, you can easily determine the number of lines in a string. This can be handy for processing text or implementing line-based operations. Consider the following code snippet:

```java
String text = "Line 1\nLine 2\nLine 3";
long lineCount = text.lines().count();
System.out.println(lineCount); // Output: 3
```

Conclusion:
Java 11 introduces several powerful string methods that simplify common tasks associated with string manipulation. By leveraging these methods, you can enhance your code readability, improve performance, and streamline your programming workflow. In this blog post, we explored the `isBlank()`, `lines()`, `strip()`, `stripLeading()`, `stripTrailing()`, `repeat()`, and `lines().count()` methods, providing examples to illustrate their usage. With a solid understanding of these new string methods, you are well-equipped to take your Java programming skills to the next level. Happy coding!

Post a Comment

Previous Post Next Post