---
title: "Normalize and split text in ONNX with C#"
url: https://recursiveai.org/onemodel/cookbook/normalize-and-split-strings-in-onnx/
description: "Lowercase text with ONNX StringNormalizer, split on a delimiter with StringSplit, and read the substrings and per-string counts in C#."
version: "1.5.0"
---

# Normalize and split text in ONNX with C#

**OneModel** is a free, open-source .NET library from Recursive AI for running ONNX models, including scikit-learn pipelines and XGBoost and LightGBM models. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/normalize-and-split-strings-in-onnx/normalize-and-split-strings-in-onnx.zip)

`split.onnx` (in the zip) lowercases text and splits it at commas. The graph uses ONNX opset 20. [See how it was made](#what-shape-does-onnx-stringnormalizer-accept).

```shell
dotnet add package RecursiveAI.OneModel
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneModel;
using var model = Model.Load("split.onnx");
using var inputs = new ModelInputs();
inputs.Add("text", ["RED,BLUE"]);
using var result = model.Run(inputs);
var pieces = result.Strings("pieces");
Console.WriteLine(
    string.Join(", ", pieces));
var counts = result.Int64s("counts");
Console.WriteLine(counts[0]);
```

Unzip [the example](https://recursiveai.org/onemodel/cookbook/normalize-and-split-strings-in-onnx/normalize-and-split-strings-in-onnx.zip) and run `dotnet run`, or create a project with `dotnet new console`, add the package, paste the snippet into `Program.cs` with `split.onnx` from the zip next to it, then run `dotnet run`.

Prints:

```text
red, blue
2
```

## The full program

```csharp
using RecursiveAI.OneModel;

using var model = Model.Load("split.onnx");
using var inputs = new ModelInputs();
inputs.Add("text", ["RED,BLUE", "GREEN"]);
using var result = model.Run(inputs);
var pieces = result.Strings("pieces");
var counts = result.Int64s("counts");
int width = result.ShapeOf("pieces")[1];
for (int row = 0; row < counts.Length; row++)
{
    int count = (int)counts[row];
    var words =
        pieces.AsSpan(row * width, count);
    string line = string.Join(" | ", words);
    Console.WriteLine($"{count}: {line}");
}
```

`dotnet run` prints:

```text
2: red | blue
1: green
```

The full program passes `"RED,BLUE"` and `"GREEN"`. `pieces` has shape `[2, 2]`: the second row is padded to match the first. `counts` is `[2, 1]`, so the program prints only the real pieces in each row.

`StringNormalizer` rejects a scalar or a matrix with more than one row; this graph declares a rank-one input, a list of strings.

## Variations

Replace the `inputs.Add` line in the full program to see the difference between an empty field and padding:

```csharp
inputs.Add("text", ["RED,,BLUE", "GREEN"]);
```

```text
3: red |  | blue
1: green
```

The empty field between commas counts as a piece. The two padding entries after `green` do not.

To inspect your own export, replace `"your-model.onnx"` with your model’s path. Use the listed names in your `Add` and output calls, as shown in [the named inputs and outputs recipe](https://recursiveai.org/onemodel/cookbook/named-inputs-and-outputs/).

```csharp
using RecursiveAI.OneModel;

using var model = Model.Load("your-model.onnx");
foreach (var input in model.Session.Inputs)
    Console.WriteLine(
        $"In: {input.Name} ({input.Kind})");
foreach (var output in model.Session.Outputs)
    Console.WriteLine(
        $"Out: {output.Name} ({output.Kind})");
```

## Related recipes

- [Validate ONNX strings with RegexFullMatch](https://recursiveai.org/onemodel/cookbook/validate-onnx-string-input-with-regex-full-match/)
- [Use named inputs and outputs in an ONNX model](https://recursiveai.org/onemodel/cookbook/named-inputs-and-outputs/)

## The short answer

Connect `StringNormalizer` to `StringSplit`. Use `case_change_action="LOWER"` and set a delimiter such as `","`. In C#, read the split text with `Strings` and the per-input counts with `Int64s`. For more than one input string, use `ShapeOf("pieces")` to find the row width and the counts to skip padding.

## What shape does ONNX StringNormalizer accept?

[`StringNormalizer`](https://onnx.ai/onnx/operators/onnx__StringNormalizer.html) takes `[C]` or `[1, C]`, where `C` is the number of strings. The example uses `[N]`, a flat list of `N` strings, so the split result is a two-dimensional table. `StringNormalizer` has been available since opset 10, but the graph uses opset 20 to include `StringSplit`. OneModel applies case changes using invariant culture; the sample uses ASCII words and doesn’t depend on machine-specific casing rules.

`StringNormalizer` can remove whole elements via its `stopwords` attribute. There are no stopwords here. A stopword filter on this graph’s first node would look at each original string, not the comma-separated pieces in it, because splitting hasn’t happened yet.

The script builds two nodes: one lowercases the strings, the other splits them. Save it as `export.py` or use the copy in the zip. To rebuild `split.onnx`, run these commands in a Python environment from the folder containing the script. The C# program uses the supplied model and doesn’t need Python.

```shell
python -m pip install onnx==1.23.2
python export.py
```

```python
import onnx
from onnx import TensorProto as T
from onnx import helper as h

normalize = h.make_node(
    "StringNormalizer", ["text"], ["lower"],
    case_change_action="LOWER", locale="C",
)
split = h.make_node(
    "StringSplit", ["lower"],
    ["pieces", "counts"], delimiter=",",
)
value_info = h.make_tensor_value_info
text = value_info("text", T.STRING, ["N"])
pieces = value_info(
    "pieces", T.STRING, ["N", "W"],
)
counts = value_info("counts", T.INT64, ["N"])
graph = h.make_graph(
    [normalize, split],
    "normalize_and_split",
    [text], [pieces, counts],
)
model = h.make_model(
    graph,
    opset_imports=[h.make_opsetid("", 20)],
    ir_version=9,
)
onnx.checker.check_model(model)
onnx.save(model, "split.onnx")
```

## How do I read StringSplit’s padded output and counts?

[`StringSplit`](https://onnx.ai/onnx/operators/onnx__StringSplit.html) adds one dimension for the pieces. With a vector input as above, that makes one row per string and enough columns for the longest split. OneModel returns the strings in row-major order: `red`, `blue`, `green`, then `""`. Get the starting index by multiplying the row by the width. The corresponding count tells you how many entries to read. Don’t drop empty strings to remove padding: consecutive explicit delimiters can produce real empty fields.

If you set `maxsplit`, the last piece can contain the unsplit remainder. If you omit the delimiter or set it to the empty string, [OneModel splits on runs of spaces](https://recursiveai.org/onemodel/docs/ops-strings/#operators), not on tabs or newlines. Use an explicit delimiter when a format defines one.
