Ends With#
Strings / Ends With
Can Read
Cannot Write
Description#
Returns True if a text finishes with another one
Parameters#
| Name | Description |
|---|---|
| Text | The text that is checked |
| Suffix | The text expected at the end |
| Ignore Case | Whether upper and lower case characters are considered the same |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Ends With")]
[Category("Strings/Ends With")]
[Description("Returns True if a text finishes with another one")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeArrowRight))]
[Parameter("Text", "The text that is checked")]
[Parameter("Suffix", "The text expected at the end")]
[Parameter("Ignore Case", "Whether upper and lower case characters are considered the same")]
[Keywords("String", "Suffix", "Finishes", "Tail", "Extension", "Find")]
[Serializable]
public class TypeBoolStringEndsWith : TypeBool
{
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetString m_Suffix = new GetString();
[SerializeField] private GetBool m_IgnoreCase = new GetBool(false);
public override bool GetPreview(GameObject source) => EndsWith(
this.m_Text.GetPreview(source),
this.m_Suffix.GetPreview(source),
this.m_IgnoreCase.GetPreview(source)
);
public override bool Get(Args args) => EndsWith(
this.m_Text.Get(args),
this.m_Suffix.Get(args),
this.m_IgnoreCase.Get(args)
);
private static bool EndsWith(string text, string suffix, bool ignoreCase)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(suffix)) return false;
return text.EndsWith(suffix, ignoreCase
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal);
}
public override string ToString()
{
return $"{this.m_Text} ends with {this.m_Suffix}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}