---
tags:
    - "String"
    - "Explode"
    - "Divide"
    - "Parts"
    - "Pieces"
    - "Amount"
---

# Split Count

[Strings](index.md) / **Split Count**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

How many pieces a text is broken into when splitting it by a separator

## Parameters

| Name | Description |
| :--- | :---------- |
| Text | The text that is split |
| Separator | The text that marks the boundary between each piece |

## Code

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

namespace VisualScript.Runtime
{
    [Get]

    [Title("Split Count")]
    [Category("Strings/Split Count")]

    [Description("How many pieces a text is broken into when splitting it by a separator")]
    [Image(typeof(IconTally), ColorTheme.Type.Yellow, typeof(BadgeList))]

    [Parameter("Text", "The text that is split")]
    [Parameter("Separator", "The text that marks the boundary between each piece")]

    [Keywords("String", "Explode", "Divide", "Parts", "Pieces", "Amount")]

    [Serializable]
    public class TypeNumberStringSplitCount : TypeNumber
    {
        [SerializeField] private GetString m_Text = new GetString();
        [SerializeField] private GetString m_Separator = new GetString(",");

        public override double GetPreview(GameObject source) => SplitCount(
            this.m_Text.GetPreview(source),
            this.m_Separator.GetPreview(source)
        );

        public override double Get(Args args) => SplitCount(
            this.m_Text.Get(args),
            this.m_Separator.Get(args)
        );

        private static double SplitCount(string text, string separator)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            if (string.IsNullOrEmpty(separator)) return 1;

            int count = 1;
            int index = text.IndexOf(separator, StringComparison.Ordinal);

            while (index >= 0)
            {
                count += 1;
                index = text.IndexOf(separator, index + separator.Length, StringComparison.Ordinal);
            }

            return count;
        }

        public override string ToString()
        {
            return $"Split {this.m_Text} by {this.m_Separator}";
        }

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

```

