First Characters#
Operations / Extract / First Characters
Can Read
Cannot Write
Description#
Extracts the leftmost characters of a text
Parameters#
| Name | Description |
|---|---|
| Text | The text to extract the characters from |
| Amount | How many characters are taken from the beginning |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("First Characters")]
[Category("Operations/Extract/First Characters")]
[Description("Extracts the leftmost characters of a text")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeArrowLeft))]
[Parameter("Text", "The text to extract the characters from")]
[Parameter("Amount", "How many characters are taken from the beginning")]
[Keywords("Left", "Start", "Begin", "Head", "Take", "Text")]
[Serializable]
public class TypeStringFirstCharacters : TypeString
{
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetInteger m_Amount = new GetInteger(1);
public override string GetPreview(GameObject source) => First(
this.m_Text.GetPreview(source),
(int) this.m_Amount.GetPreview(source)
);
public override string Get(Args args) => First(
this.m_Text.Get(args),
this.m_Amount.GetInt(args)
);
private static string First(string text, int amount)
{
if (string.IsNullOrEmpty(text) || amount <= 0) return string.Empty;
return text.Substring(0, Mathf.Min(amount, text.Length));
}
public override string ToString()
{
return $"First {this.m_Amount} of {this.m_Text}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}