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