Index Of#
Strings / Index Of
Can Read
Cannot Write
Description#
The index of the first occurrence of a text inside another one. Returns -1 if not found
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("Index Of")]
[Category("Strings/Index Of")]
[Description("The index of the first occurrence of a text inside another one. Returns -1 if not found")]
[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", "Find", "Search", "Position", "Location", "Contains")]
[Serializable]
public class TypeNumberStringIndexOf : TypeNumber
{
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetString m_Find = new GetString();
[SerializeField] private GetBool m_IgnoreCase = new GetBool(false);
public override double GetPreview(GameObject source) => IndexOf(
this.m_Text.GetPreview(source),
this.m_Find.GetPreview(source),
this.m_IgnoreCase.GetPreview(source)
);
public override double Get(Args args) => IndexOf(
this.m_Text.Get(args),
this.m_Find.Get(args),
this.m_IgnoreCase.Get(args)
);
private static double IndexOf(string text, string find, bool ignoreCase)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(find)) return -1;
StringComparison comparison = ignoreCase
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
return text.IndexOf(find, comparison);
}
public override string ToString()
{
return $"Index of {this.m_Find} in {this.m_Text}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}