Split Count#
Strings / Split Count
Can Read
Cannot Write
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#
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;
}
}