Insert#
Operations / Composing / Insert
Can Read
Cannot Write
Description#
Inserts a text inside another one at the specified character index
Parameters#
| Name | Description |
|---|---|
| Text | The text that receives the insertion |
| Insert | The text that is inserted |
| Index | The character index where the insertion starts. Clamped to the text length |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Insert")]
[Category("Operations/Composing/Insert")]
[Description("Inserts a text inside another one at the specified character index")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeIndex))]
[Parameter("Text", "The text that receives the insertion")]
[Parameter("Insert", "The text that is inserted")]
[Parameter("Index", "The character index where the insertion starts. Clamped to the text length")]
[Keywords("Add", "Splice", "Between", "Middle", "Text")]
[Serializable]
public class TypeStringInsert : TypeString
{
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetString m_Insert = new GetString();
[SerializeField] private GetInteger m_Index = new GetInteger(0);
public override string GetPreview(GameObject source) => Insert(
this.m_Text.GetPreview(source),
this.m_Insert.GetPreview(source),
(int) this.m_Index.GetPreview(source)
);
public override string Get(Args args) => Insert(
this.m_Text.Get(args),
this.m_Insert.Get(args),
this.m_Index.GetInt(args)
);
private static string Insert(string text, string insert, int index)
{
text ??= string.Empty;
if (string.IsNullOrEmpty(insert)) return text;
return text.Insert(Mathf.Clamp(index, 0, text.Length), insert);
}
public override string ToString()
{
return $"Insert {this.m_Insert} in {this.m_Text}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}