# Strings
source: https://docs.chalk.ai/docs/chalkdf/expressions/strings

## Clean transaction descriptions, match text, extract identifiers, and redact values.

A payment processor supplies a free-text memo with each transaction. Preserve the source memo for reconciliation and derive clean fields for grouping, filters, and display. These examples build on the same four transactions.

### Normalize case and whitespace

Strip padding before lowercasing. Display the original in quotes so the spaces at either end are visible beside the cleaned value.

```python
from chalk import _, functions as F
from chalkdf import DataFrame

tx = DataFrame({
    "id": [1, 2, 3, 4],
    "memo": [" WEB:Order-0042 ", "web:order-0178", "POS:refund-19", None],
})

clean = tx.with_columns(F.lower(F.trim(_.memo)).alias("text"))
quoted = ('"' + _.memo + '"').alias("quoted_memo")
clean.select("id", quoted, "text").run()
```

| id · `int64` | quoted_memo · `large_string` | text · `large_string` |
| ------------ | ---------------------------- | --------------------- |
| 1            | " WEB:Order-0042 "           | web:order-0042        |
| 2            | "web:order-0178"             | web:order-0178        |
| 3            | "POS:refund-19"              | pos:refund-19         |
| 4            | null                         | null                  |

A missing memo stays null through both operations. Fill it only when the application needs a display value.

### Filter by a text prefix

To select all web transactions, match a literal prefix against the clean field.

```python
clean.filter(F.starts_with(_.text, "web:")).select("id", quoted).run()
```

| id · `int64` | quoted_memo · `large_string` |
| ------------ | ---------------------------- |
| 1            | " WEB:Order-0042 "           |
| 2            | "web:order-0178"             |

Testing the original field would miss the padded uppercase memo. Filtering on the normalized field still lets you return the original memo.

### Match a pattern and extract an identifier

The channel is everything before the colon. An order ID has a more specific pattern. Use a capture group to extract just its digits.

```python
parsed = clean.project({
    "id": _.id,
    "channel": F.split_part(_.text, ":", 0),
    "is_order": F.regexp_like(_.text, r"order-[0-9]+"),
    "order_id": F.regexp_extract(_.text, r"order-([0-9]+)", 1),
})
parsed.run()
```

| id · `int64` | channel · `large_string` | is_order · `bool` | order_id · `large_string` |
| ------------ | ------------------------ | ----------------- | ------------------------- |
| 1            | web                      | True              | 0042                      |
| 2            | web                      | True              | 0178                      |
| 3            | pos                      | False             | null                      |
| 4            | null                     | null              | null                      |

The pattern test returns a boolean, while extraction produces a field you can join or group on. Keep order IDs as text to preserve their leading zeros.

### Redact before displaying

To keep the channel and event type in a support view, replace each run of digits while retaining the original memo on the source frame.

```python
clean.project({
    "id": _.id,
    "display_memo": F.coalesce(F.regexp_replace(_.text, r"[0-9]+", "***"), "(missing)"),
}).run()
```

| id · `int64` | display_memo · `large_string` |
| ------------ | ----------------------------- |
| 1            | web:order-***                 |
| 2            | web:order-***                 |
| 3            | pos:refund-***                |
| 4            | (missing)                     |

The fallback applies to the display field. The missing transaction remains distinguishable when you group by the original field.

### Summarize by channel

Group on the parsed field after removing transactions without a known channel.

```python
parsed.filter(F.is_not_null(_.channel)).agg(
    ["channel"], _.count().alias("transactions")
).order_by("channel").run()
```

| channel · `large_string` | transactions · `int64` |
| ------------------------ | ---------------------- |
| pos                      | 1                      |
| web                      | 2                      |

The two web memos now contribute to the same channel total.

### Build a display label from parts

Take the channel and the original order identifier from the parsed fields. Join the two with a separator for display.

```python
parsed.filter(_.is_order).project({
    "label": _.channel + "/" + _.order_id,
}).run()
```

| label · `large_string` |
| --- |
| web/0042 |
| web/0178 |

The labels preserve the leading zeros of the captured order IDs. Keep the fields separate for joins on channel or order ID.

### Slice a field with a fixed layout

Use a slice when a feed specifies the position of a field. In this feed the first three characters identify the issuing office.

```python
codes = DataFrame({"code": ["SFO0042", "LAX0178", None]})
codes.project({
    "code": _.code, "office": F.substr(_.code, 0, 3),
    "serial": F.substr(_.code, 3),
}).run()
```

| code · `string` | office · `string` | serial · `string` |
| --------------- | ----------------- | ----------------- |
| SFO0042         | SFO               | 0042              |
| LAX0178         | LAX               | 0178              |
| null            | null              | null              |

The start offset is zero-based. Omitting the length takes the rest of the string. Use a capture group when the prefix varies in length.

See [string functions](/api-docs#section-chalk-functions) and [missing data](/docs/chalkdf/expressions/missing-data).





