Format#
Operations / Composing / Format
Can Read
Cannot Write
Description#
Replaces every '{0}', '{1}', ... placeholder of a template with the values below
Parameters#
| Name | Description |
|---|---|
| Template | The text with the numbered placeholders to replace |
| Values | The list of values that replace each indexed placeholder |
Code#
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Format")]
[Category("Operations/Composing/Format")]
[Description("Replaces every '{0}', '{1}', ... placeholder of a template with the values below")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeBraces))]
[Parameter("Template", "The text with the numbered placeholders to replace")]
[Parameter("Values", "The list of values that replace each indexed placeholder")]
[Keywords("Template", "Interpolate", "Placeholder", "Compose", "Replace", "Text")]
[Serializable]
public class TypeStringFormat : TypeString
{
[SerializeField] private GetString m_Template = new GetString("{0}");
[SerializeField] private List<GetString> m_Values = new List<GetString>();
[NonSerialized] private readonly StringBuilder m_Builder = new StringBuilder();
public override string Get(Args args)
{
string template = this.m_Template.Get(args);
if (string.IsNullOrEmpty(template)) return string.Empty;
this.m_Builder.Clear();
this.m_Builder.Append(template);
for (int i = 0; i < this.m_Values.Count; ++i)
{
GetString value = this.m_Values[i];
this.m_Builder.Replace($"{{{i}}}", value?.Get(args) ?? string.Empty);
}
return this.m_Builder.ToString();
}
public override string ToString()
{
return $"Format {this.m_Template}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}