---
tags:
    - "Slice"
    - "Portion"
    - "Cut"
    - "Range"
    - "Piece"
    - "Text"
---

# Substring

[Operations](../index.md) / [Extract](index.md) / **Substring**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

Extracts a portion of a text starting at an index and with a certain length

## Parameters

| Name | Description |
| :--- | :---------- |
| Text | The text to extract the portion from |
| From | The character index where the portion starts |
| Length | How many characters are extracted. Values out of range are clamped |

## Code

```cs
using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]

    [Title("Substring")]
    [Category("Operations/Extract/Substring")]

    [Description("Extracts a portion of a text starting at an index and with a certain length")]
    [Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeMagnifier))]

    [Parameter("Text", "The text to extract the portion from")]
    [Parameter("From", "The character index where the portion starts")]
    [Parameter("Length", "How many characters are extracted. Values out of range are clamped")]

    [Keywords("Slice", "Portion", "Cut", "Range", "Piece", "Text")]

    [Serializable]
    public class TypeStringSubstring : TypeString
    {
        [SerializeField] private GetString m_Text = new GetString();
        [SerializeField] private GetInteger m_From = new GetInteger(0);
        [SerializeField] private GetInteger m_Length = new GetInteger(1);

        public override string GetPreview(GameObject source) => Substring(
            this.m_Text.GetPreview(source),
            (int) this.m_From.GetPreview(source),
            (int) this.m_Length.GetPreview(source)
        );

        public override string Get(Args args) => Substring(
            this.m_Text.Get(args),
            this.m_From.GetInt(args),
            this.m_Length.GetInt(args)
        );

        private static string Substring(string text, int from, int length)
        {
            if (string.IsNullOrEmpty(text) || length <= 0) return string.Empty;

            from = Mathf.Clamp(from, 0, text.Length);
            length = Mathf.Clamp(length, 0, text.Length - from);

            return text.Substring(from, length);
        }

        public override string ToString()
        {
            return $"Substring {this.m_Text} ({this.m_From}, {this.m_Length})";
        }

        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}

```

