Character At#
Operations / Extract / Character At
Can Read
Cannot Write
Description#
Returns the single character found at the specified index. Empty if out of range
Parameters#
| Name | Description |
|---|---|
| Text | The text to read the character from |
| Index | The zero based index of the character |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Character At")]
[Category("Operations/Extract/Character At")]
[Description("Returns the single character found at the specified index. Empty if out of range")]
[Image(typeof(IconString), ColorTheme.Type.Yellow, typeof(BadgeString))]
[Parameter("Text", "The text to read the character from")]
[Parameter("Index", "The zero based index of the character")]
[Keywords("Letter", "Index", "Position", "Char", "Text")]
[Serializable]
public class TypeStringCharacterAt : TypeString
{
[SerializeField] private GetString m_Text = new GetString();
[SerializeField] private GetInteger m_Index = new GetInteger(0);
public override string GetPreview(GameObject source) => CharacterAt(
this.m_Text.GetPreview(source),
(int) this.m_Index.GetPreview(source)
);
public override string Get(Args args) => CharacterAt(
this.m_Text.Get(args),
this.m_Index.GetInt(args)
);
private static string CharacterAt(string text, int index)
{
if (string.IsNullOrEmpty(text)) return string.Empty;
if (index < 0 || index >= text.Length) return string.Empty;
return text[index].ToString();
}
public override string ToString()
{
return $"{this.m_Text}[{this.m_Index}]";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}