> For the complete documentation index, see [llms.txt](https://docs.wisej.com/ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wisej.com/ai/components/api/services/itextsplitterservice/wisej.ai.services.recursivecharactertextsplitterservice.md).

# RecursiveCharacterTextSplitterService

Namespace: **Wisej.AI.Services**

Assembly: **Wisej.AI** (3.5.0.0)

* [TextSplitterServiceBase](/ai/components/api/services/itextsplitterservice/wisej.ai.services.textsplitterservicebase.md)
  * [RecursiveCharacterTextSplitterService](/ai/components/api/services/itextsplitterservice/wisej.ai.services.recursivecharactertextsplitterservice.md)

A service for recursively splitting text into chunks based on specified separators and chunk size constraints. This service attempts to split text by different characters to find an optimal separation strategy.

{% tabs %}
{% tab title="C#" %}

```csharp
public class RecursiveCharacterTextSplitterService : TextSplitterServiceBase
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class RecursiveCharacterTextSplitterService
    Inherits TextSplitterServiceBase
```

{% endtab %}
{% endtabs %}

## Constructors

### ![](/files/ptrKjmmRoQB76pvrIqh0) RecursiveCharacterTextSplitterService(chunkSize, chunkOverlap, separators, lengthFunction)

Initializes a new instance of the [RecursiveCharacterTextSplitterService](/ai/components/api/services/itextsplitterservice/wisej.ai.services.recursivecharactertextsplitterservice.md) class with specified separators, chunk size, overlap, and length function.

| Name               | Type                                                                        | Description                                                                                                                 |
| ------------------ | --------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| **chunkSize**      | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)                 | The maximum size of each text chunk. Defaults to 1000 if not specified.                                                     |
| **chunkOverlap**   | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)                 | The allowed overlap size between chunks. Defaults to 200 if not specified.                                                  |
| **separators**     | [String\[\]](https://docs.microsoft.com/dotnet/api/system.string)           | The array of string separators to be used for splitting the text. Default is an array containing "\n\n", "\n", " ", and "". |
| **lengthFunction** | [Func\<String, Int32>](https://docs.microsoft.com/dotnet/api/system.func-2) | A function to determine the length of the text, which will be used to comply with the chunk size constraint.                |

## Methods

### ![](/files/ptrKjmmRoQB76pvrIqh0) Split(text)

Splits the given text into chunks using the defined separators and chunk size constraints.

| Parameter | Type                                                          | Description                       |
| --------- | ------------------------------------------------------------- | --------------------------------- |
| **text**  | [String](https://docs.microsoft.com/dotnet/api/system.string) | The text to be split into chunks. |

**Returns:** [String\[\]](https://docs.microsoft.com/dotnet/api/system.string). An array of strings, where each string represents a chunk of the original text.

This method seeks to split the provided text based on the list of separators, starting with more significant separators and moving to less significant ones. If none of the separators are found, it will treat the text as a sequence of individual characters.\
The process ensures that each chunk does not exceed the specified chunk size. If a text segment is larger than the specified chunk size, it will be recursively split further.\
Usage example:

```csharp

  var separators = new[] { ",", " ", "\n" };
  var splitterService = new RecursiveCharacterTextSplitterService(separators, 500, 100, text => text.Length);
  string textToSplit = "This is a sample text that will be split into smaller chunks.";
  string[] chunks = splitterService.Split(textToSplit);
  foreach (var chunk in chunks)
  {
   Console.WriteLine(chunk);
  }

```

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the *text* is null.

## Implements

| Name                                                                        | Description                                                          |
| --------------------------------------------------------------------------- | -------------------------------------------------------------------- |
| [ITextSplitterService](/ai/components/api/services/itextsplitterservice.md) | Represents a service for splitting text into an array of substrings. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.wisej.com/ai/components/api/services/itextsplitterservice/wisej.ai.services.recursivecharactertextsplitterservice.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
