Join Collection#
Operations / Composing / Join Collection
Can Read
Cannot Write
Description#
Converts every item of a collection to text and joins them using a separator
Parameters#
| Name | Description |
|---|---|
| Collection | The collection whose items are converted to text |
| Separator | The text placed between each pair of items |
Code#
using System;
using System.Text;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Join Collection")]
[Category("Operations/Composing/Join Collection")]
[Description("Converts every item of a collection to text and joins them using a separator")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeList))]
[Parameter("Collection", "The collection whose items are converted to text")]
[Parameter("Separator", "The text placed between each pair of items")]
[Keywords("Implode", "List", "Array", "Concatenate", "Merge", "Separator", "Comma")]
[Serializable]
public class TypeStringJoin : TypeString
{
[SerializeField] private GetCollection m_Collection = new GetCollection();
[SerializeField] private GetString m_Separator = new GetString(", ");
[NonSerialized] private readonly StringBuilder m_Builder = new StringBuilder();
public override string Get(Args args)
{
using Args scoped = Args.Get(args);
int count = this.m_Collection.Capture(scoped);
if (count == 0) return string.Empty;
string separator = this.m_Separator.Get(args) ?? string.Empty;
this.m_Builder.Clear();
for (int i = 0; i < count; ++i)
{
if (i > 0) this.m_Builder.Append(separator);
if (ArgsCollectionParameters.TryGetItem(this.m_Collection, scoped, i, out string item))
{
this.m_Builder.Append(item);
}
}
return this.m_Builder.ToString();
}
public override string ToString()
{
return $"Join {this.m_Collection}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}