Home Explore Blog CI



nushell

4th chunk of `book/working_with_strings.md`
e162a2b5c27ca30b72f375b7eb99d518e8dbfaec8e59ae360000000100000cfd
So if you have something like this in your `config.nu`, `x` will be `"2.0 KB"` even if your config says to use
`MB` for all file sizes (datetimes will similarly use the default config).

```nu
const x = $"(2kb)"
```

## Splitting Strings

The [`split row`](/commands/docs/split_row.md) command creates a list from a string based on a delimiter.

```nu
"red,green,blue" | split row ","
# => ╭───┬───────╮
# => │ 0 │ red   │
# => │ 1 │ green │
# => │ 2 │ blue  │
# => ╰───┴───────╯
```

The [`split column`](/commands/docs/split_column.md) command will create a table from a string based on a delimiter. This applies generic column names to the table.

```nu
"red,green,blue" | split column ","
# => ╭───┬─────────┬─────────┬─────────╮
# => │ # │ column1 │ column2 │ column3 │
# => ├───┼─────────┼─────────┼─────────┤
# => │ 0 │ red     │ green   │ blue    │
# => ╰───┴─────────┴─────────┴─────────╯
```

Finally, the [`split chars`](/commands/docs/split_chars.md) command will split a string into a list of characters.

```nu
'aeiou' | split chars
# => ╭───┬───╮
# => │ 0 │ a │
# => │ 1 │ e │
# => │ 2 │ i │
# => │ 3 │ o │
# => │ 4 │ u │
# => ╰───┴───╯
```

## The [`str`](/commands/docs/str.md) command

Many string functions are subcommands of the [`str`](/commands/docs/str.md) command. You can get a full list using `help str`.

For example, you can look if a string contains a particular substring using [`str contains`](/commands/docs/str_contains.md):

```nu
"hello world" | str contains "o wo"
# => true
```

(You might also prefer, for brevity, the `=~` operator (described below).)

### Trimming Strings

You can trim the sides of a string with the [`str trim`](/commands/docs/str_trim.md) command. By default, the [`str trim`](/commands/docs/str_trim.md) commands trims whitespace from both sides of the string. For example:

```nu
'       My   string   ' | str trim
# => My   string
```

You can specify on which side the trimming occurs with the `--right` and `--left` options. (`-r` and `-l` being the short-form options respectively)

To trim a specific character, use `--char <Character>` or `-c <Character>` to specify the character to trim.

Here's an example of all the options in action:

```nu
'=== Nu shell ===' | str trim -r -c '='
# => === Nu shell
```

### Substrings

Substrings are slices of a string. They have a startpoint and an endpoint. Here's an example of using a substring:

```nu
'Hello World!' | str index-of 'o'
# => 4
'Hello World!' | str index-of 'r'
# => 8
'Hello World!' | str substring 4..8
# => o Wo
```

### String Padding

With the [`fill`](/commands/docs/fill.md) command you can add padding to a string. Padding adds characters to string until it's a certain length. For example:

```nu
'1234' | fill -a right -c '0' -w 10
# => 0000001234
'1234' | fill -a left -c '0' -w 10 | str length
# => 10
```

### Reversing Strings

This can be done easily with the [`str reverse`](/commands/docs/str_reverse.md) command.

Title: String Splitting and Manipulation with the `str` Command in Nushell
Summary
This section details how to split strings into lists or tables using `split row`, `split column`, and `split chars`. It then introduces the `str` command and its subcommands, providing examples for checking substrings with `str contains`, trimming strings with `str trim`, extracting substrings with `str index-of` and `str substring`, padding strings with `fill`, and reversing strings with `str reverse`.