# prefer-string-slice

Prefer `String.slice` over the legacy `String.substring` operation

`String.substring` swaps reversed bounds and clamps negative bounds, which differs from collection slicing.
Instead, you SHOULD use `String.slice` for the same start-inclusive, end-exclusive convention as arrays.

Review negative or dynamically ordered bounds because `slice` preserves their direction and interprets negative values from the end.

- Category: style
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
function prefix(text: string, end: isize): string {
    return text.substring(0, end);
}
```

## Accepted

```ds title="main.ds"
function prefix(text: string, end: isize): string {
    return text.slice(0, end);
}
```

## Prior art

- [eslint-plugin-unicorn · prefer-string-slice](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-string-slice.md)

[language/linter/src/rules/style/prefer_string_slice.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/prefer_string_slice.rs#L8)
