Split Text#
Strings / Split Text
Can Read
Cannot Write
Description#
Breaks a text into a collection of pieces using a separator
Parameters#
| Name | Description |
|---|---|
| Text | The text that is split |
| Separator | The text that marks the boundary between each piece |
| Remove Empty | Whether pieces without any character are dropped from the result |
Code#
using System;
using System.Collections.Generic;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Split Text")]
[Category("Strings/Split Text")]
[Description("Breaks a text into a collection of pieces using a separator")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeList))]
[Parameter("Text", "The text that is split")]
[Parameter("Separator", "The text that marks the boundary between each piece")]
[Parameter("Remove Empty", "Whether pieces without any character are dropped from the result")]
[Keywords("String", "Explode", "Divide", "Parts", "Pieces", "Separator", "Comma")]
[Serializable]
public class TypeCollectionStringSplit : TTypeCollection<string>
{
// EXPOSED MEMBERS: -----------------------------------------------------------------------
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetString m_Separator = new GetString(",");
[SerializeField] private GetBool m_RemoveEmpty = new GetBool(false);
// PUBLIC METHODS: ------------------------------------------------------------------------
public override string ToString()
{
return $"Split {this.m_Text} by {this.m_Separator}";
}
// PROTECTED METHODS: ---------------------------------------------------------------------
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
protected override void OnCapture(Args args, List<string> items)
{
string text = this.m_Text.Get(args);
if (string.IsNullOrEmpty(text)) return;
string separator = this.m_Separator.Get(args);
if (string.IsNullOrEmpty(separator))
{
items.Add(text);
return;
}
StringSplitOptions options = this.m_RemoveEmpty.Get(args)
? StringSplitOptions.RemoveEmptyEntries
: StringSplitOptions.None;
items.AddRange(text.Split(separator, options));
}
}
}