String.slice
String.slice
Function
Cut the string according to the starting and ending positions.
Syntax
string.slice(start, end)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| start | Int | No | 0 | Starting character position; a negative number means counting backwards from the end. |
| end | Int | No | String length | The end character position, excluding this position; a negative number means counting backwards from the end. |
Return value
| Type | Description |
|---|---|
| String | Returns the new string after interception. |
Example
text = 'hello BT' result = text.slice(0, 5) // Output: hello print result
Notes
- Truncate according to character position and will not cut off UTF-8 characters.
- Returns an empty string when start is greater than or equal to end.