# Io / Github / Biezhi / Java11 / String

## Overview

This module demonstrates the new `String` API methods introduced in **JDK 11**. It is part of a larger collection of Java 11 feature examples organized under the `io.github.biezhi.java11` package hierarchy. The module's single class, `Example`, walks through the four new `String` methods that JDK 11 added — `String.repeat(int)`, `String.lines()`, `String.strip()`, and `String.isBlank()` — with runnable code that prints their behaviour to the console.

## Key Classes

### `Example`

**Source:** [`src/main/java/io/github/biezhi/java11/string/Example.java`](src/main/java/io/github/biezhi/java11/string/Example.java) (line 16)

The sole class in this module. It is a plain utility class with no fields, no constructor, and only static methods. Every method is a self-contained demonstration of a specific JDK 11 `String` feature. The class exists purely as an educational and reference example — it is not designed to be instantiated or extended.

#### Methods

##### `writeHeader(String headerText)`

| Detail | Description |
|---|---|
| **Visibility** | `private static` |
| **Lines** | 24–30 |

A private helper that prints a formatted header to `stdout`. It uses `String.repeat(int)` (also a JDK 11 addition) to generate a separator line made of `=` characters that is four characters wider than the header text. The output looks like:

```
======
Title
======
```

This method is called by every public demonstration method, keeping the output visually consistent.

##### `demonstrateStringLines()`

| Detail | Description |
|---|---|
| **Visibility** | `public static` |
| **Lines** | 36–44 |
| **JDK 11 feature** | `String.lines()` |

Demonstrates the `String.lines()` method, which returns a `Stream<String>` of the lines contained in the string. The method creates a multi-line string (`"Hello
World
123"`), prints a header via `writeHeader()`, and then prints each line to `stdout` by iterating over the returned stream with `forEach(System.out::println)`.

`String.lines()` treats `
`, `\r
`, and `\r` as line separators and produces one element per line (excluding the separator characters themselves).

##### `demonstrateStringStrip()`

| Detail | Description |
|---|---|---|
| **Visibility** | `public static` |
| **Lines** | 49–54 |
| **JDK 11 feature** | `String.strip()` |

Demonstrates `String.strip()`, which removes leading and trailing **Unicode whitespace** characters (code points ≤ U+0020) from the string. The example string `"  biezhi.me  23333  "` is printed before and after `.strip()` to show that all leading and trailing spaces are removed in a single call.

Unlike `String.trim()` (which only strips ASCII whitespace), `strip()` recognises all Unicode whitespace defined by `Character.isWhitespace()`.

##### `demonstrateStringStripLeading()`

| Detail | Description |
|---|---|---|
| **Visibility** | `public static` |
| **Lines** | 59–64 |
| **JDK 11 feature** | `String.stripLeading()` |

Demonstrates `String.stripLeading()`, which removes only the **leading** Unicode whitespace characters from the string. The same example string is used, and only the spaces before `"biezhi.me"` are removed — the trailing spaces after `"23333"` remain.

##### `demonstrateStringStripTrailing()`

| Detail | Description |
|---|---|---|
| **Visibility** | `public static` |
| **Lines** | 69–74 |
| **JDK 11 feature** | `String.stripTrailing()` |

Demonstrates `String.stripTrailing()`, which removes only the **trailing** Unicode whitespace characters from the string. Leading spaces are preserved, trailing spaces are removed.

##### `demonstrateStringIsBlank()`

| Detail | Description |
|---|---|---|
| **Visibility** | `public static` |
| **Lines** | 79–93 |
| **JDK 11 feature** | `String.isBlank()` |

Demonstrates `String.isBlank()`, which returns `true` if the string is empty or contains only Unicode whitespace characters (spaces, tabs, newlines, etc.). The method prints four test cases:

| Input string | Expected `isBlank()` result |
|---|---|
| `""` (empty string) | `true` |
| `System.getProperty("line.separator")` (newline) | `true` |
| `"\t"` (tab) | `true` |
| `"   "` (spaces only) | `true` |

A string is considered blank when it is either empty or every character is whitespace. This is stricter than `null` checks and avoids `NullPointerException`.

##### `lines()`

| Detail | Description |
|---|---|---|
| **Visibility** | `public static` |
| **Lines** | 96–102 |

An alternative demonstration of `String.lines()` that uses Java Streams to collect the resulting lines into a `List`. It creates a multi-line string (`"Hello 
 World, I,m
biezhi."`) and calls `str.lines().collect(Collectors.toList())`, printing the resulting list. This shows how `lines()` integrates naturally with the Stream API.

##### `main(String[] args)`

| Detail | Description |
|---|---|
| **Visibility** | `public static` |
| **Lines** | 104–112 |

The entry point. By default, all demonstration methods are commented out and only `lines()` is invoked. The `writeHeader` call for a User-Agent header and all other demonstrations are left as inactive comments, suggesting this file serves as an interactive notebook where developers can uncomment individual sections to try them out.

## How It Works

The module follows a simple, consistent pattern:

1. **A demonstration method** creates a sample `String` that exercises a specific JDK 11 API.
2. **`writeHeader()`** is called to print a visual title to the console.
3. **The new method** (`strip()`, `stripLeading()`, `stripTrailing()`, `isBlank()`, `lines()`) is called on the sample string.
4. **`System.out.println`** prints the result so the developer can observe the output.

There are no data flows between methods — each is entirely self-contained. The `main()` method is the only orchestrator, and currently calls only the `lines()` method.

```mermaid
flowchart TD
    Main["Example class<br/>io.github.biezhi.java11.string"]
    Header["writeHeader()"]
    LinesDemo["demonstrateStringLines()"]
    StripDemo["demonstrateStringStrip()"]
    StripLeadingDemo["demonstrateStringStripLeading()"]
    StripTrailingDemo["demonstrateStringStripTrailing()"]
    IsBlankDemo["demonstrateStringIsBlank()"]
    LinesFn["lines()"]
    MainEntry["main()"]

    Main --> Header
    Main --> LinesDemo
    Main --> StripDemo
    Main --> StripLeadingDemo
    Main --> StripTrailingDemo
    Main --> IsBlankDemo
    Main --> LinesFn
    MainEntry --> LinesFn
```

## Data Model

This module has no data model — there are no entity classes, DTOs, or configuration objects. Every method operates on primitive `String` inputs and prints results directly. The only data structure created is a `List<String>` returned by `Collectors.toList()` inside the `lines()` method.

## Dependencies and Integration

- **Java 11 runtime** — This module requires at least JDK 11 to compile and run, because the methods demonstrated (`repeat`, `lines`, `strip`, `stripLeading`, `stripTrailing`, `isBlank`) were introduced in JDK 11.
- **`java.util.stream.Collectors`** — The `lines()` method imports `java.util.stream.Collectors` to collect the `Stream<String>` from `String.lines()` into a `List`.
- **No external dependencies** — There are no third-party libraries used. The module has no package-level dependencies declared in the evidence.
- **Sibling modules** — This module sits alongside other Java 11 feature modules (`collections`, `files`, `http`, `processor`, `time`, `trywithresources`, `var`, `interfaces`) which share the same `Example` class naming convention.

## Notes for Developers

- **Educational purpose only** — This class is a playground, not production code. Every method is a standalone demonstration with no error handling or edge-case rigor.
- **Commented-out `main()` calls** — The `main()` method currently has all demonstration calls commented out and only calls `lines()`. To test individual features, uncomment the relevant line before running.
- **`strip()` vs `trim()`** — `String.strip()` is the JDK 11 replacement for `String.trim()`. Use `strip()` when you need Unicode-aware whitespace removal; `trim()` only handles ASCII (code points ≤ 0x20).
- **`isBlank()` vs `isEmpty()`** — `isBlank()` returns `true` for strings containing only whitespace (not just empty strings). If you need to distinguish between "empty" and "whitespace-only", check `isEmpty()` first, then `isBlank()`.
- **`String.lines()` returns a `Stream`** — The stream is lazy and only processes lines when a terminal operation is applied. It does not throw on malformed input but does not guarantee that all lines are consumed if the stream is short-circuited.
- **Author and history** — The class header is credited to author `biezhi` with a date of 2018/7/10, predating the JDK 11 release itself. The methods documented here were added after the initial file creation.
