Trim#
Operations / Transform / Trim
Can Read
Cannot Write
Description#
Removes the characters found at the beginning and/or the end of a text
Parameters#
| Name | Description |
|---|---|
| Text | The text that is trimmed |
| Side | Whether the characters are removed from the start, the end or both |
| Characters | The set of characters removed. Whitespaces are used when left empty |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Trim")]
[Category("Operations/Transform/Trim")]
[Description("Removes the characters found at the beginning and/or the end of a text")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeMinus))]
[Parameter("Text", "The text that is trimmed")]
[Parameter("Side", "Whether the characters are removed from the start, the end or both")]
[Parameter("Characters", "The set of characters removed. Whitespaces are used when left empty")]
[Keywords("Whitespace", "Space", "Strip", "Clean", "Text")]
[Serializable]
public class TypeStringTrim : TypeString
{
private enum Side
{
Both,
Start,
End
}
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private Side m_Side = Side.Both;
[SerializeField] private GetString m_Characters = new GetString(string.Empty);
public override string GetPreview(GameObject source) => Trim(
this.m_Text.GetPreview(source),
this.m_Side,
this.m_Characters.GetPreview(source)
);
public override string Get(Args args) => Trim(
this.m_Text.Get(args),
this.m_Side,
this.m_Characters.Get(args)
);
private static string Trim(string text, Side side, string characters)
{
if (string.IsNullOrEmpty(text)) return string.Empty;
char[] set = string.IsNullOrEmpty(characters) ? null : characters.ToCharArray();
return side switch
{
Side.Both => set == null ? text.Trim() : text.Trim(set),
Side.Start => set == null ? text.TrimStart() : text.TrimStart(set),
Side.End => set == null ? text.TrimEnd() : text.TrimEnd(set),
_ => text
};
}
public override string ToString()
{
return $"Trim {this.m_Text}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}