Contains#
Strings / Contains
Can Read
Cannot Write
Description#
Returns True if a text is found anywhere inside another one
Parameters#
| Name | Description |
|---|---|
| Text | The text where the occurrence is searched |
| Find | The text that is searched for |
| 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("Contains")]
[Category("Strings/Contains")]
[Description("Returns True if a text is found anywhere inside another one")]
[Image(typeof(IconSearch), ColorTheme.Type.Yellow, typeof(BadgeString))]
[Parameter("Text", "The text where the occurrence is searched")]
[Parameter("Find", "The text that is searched for")]
[Parameter("Ignore Case", "Whether upper and lower case characters are considered the same")]
[Keywords("String", "Has", "Includes", "Find", "Search", "Substring")]
[Serializable]
public class TypeBoolStringContains : TypeBool
{
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetString m_Find = new GetString();
[SerializeField] private GetBool m_IgnoreCase = new GetBool(false);
public override bool GetPreview(GameObject source) => Contains(
this.m_Text.GetPreview(source),
this.m_Find.GetPreview(source),
this.m_IgnoreCase.GetPreview(source)
);
public override bool Get(Args args) => Contains(
this.m_Text.Get(args),
this.m_Find.Get(args),
this.m_IgnoreCase.Get(args)
);
private static bool Contains(string text, string find, bool ignoreCase)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(find)) return false;
StringComparison comparison = ignoreCase
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
return text.IndexOf(find, comparison) >= 0;
}
public override string ToString()
{
return $"{this.m_Text} contains {this.m_Find}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}