CharAt in C#: A Practical Guide
Introduction
If you use Unity to make a word-based game like, idk, Wordle, having a method like Javascriptâs CharAt in C# is helpful.
CharAt returns the character at a specific index in a string. For example, the string âHola mamiâ is made up of 9 characters, "H", "o", "l", "a", " ", "m", "a", "m", and "i". In Javascript, you can use CharAt to get the character at a specific position. In this quick article, I will explain everything you need to know about using CharAt in C#.
Quick help
- Use the index accessor
[ ]to get the character at a specific index in a string. - To get the first character in a string, use
myString[0]. - To get the last character in a string, use
myString[myString.Length - 1].
How does CharAt work in Javascript?
For example, if we want to get the character 'oâ, from the string "Hola mami", we would use charAt with an index of 1.
let text = "Hola mami";
let letter = text.charAt(1);
The CharAt equal in C# is similar and even more straightforward. Instead of an extension method (CharAt), use the index accessor.
How do I use CharAt in C#?
In this example, I will show you how to get a character from a string in a complete component. As I mentioned, you use the index accessor. In other words, you treat the string like an array and use the array index.
using UnityEngine;
public class CharAtExample : MonoBehaviour
{
public string hola = "Hola mami";
void Start()
{
char o = hola[1];
Debug.Log(o);
}
}
The array indexer is like an implicit method that returns the value of that item in the array. If you still need clarification, donât worry! I wrote an interactive demo script to help you apply this concept.
Interactive demo script
I wrote a small script to help you play around with this concept. Create a new component using this script. Then, add the script to an object in your scene. This component will write the result of the C# version of CharAt to the console. I wrap the index for you so you never encounter out-of-bounds exceptions.
using UnityEngine;
[ExecuteAlways]
public class InteractiveCharAt : MonoBehaviour
{
public string testString = "Hi, mom!";
public int indexToFetch = 0;
private void Update()
{
if (indexToFetch >= testString.Length)
indexToFetch = 0;
if (indexToFetch < 0)
indexToFetch = testString.Length - 1;
Debug.Log(testString[indexToFetch]);
}
}
In the inspector, try changing the test string to different values.
What happens if you set the string to "Michael is the goat of all time"? (Thatâs me, btw).
What happens when you change the index around?
Does the index need to be less than the string length?
Yes. Put an index greater than or equal to the string length. You will get an error. âIndexOutOfRangeException: Index was outside the bounds of the array.â This error indicates that the index is greater than the arrayâs length.
To resolve this error, ensure that your index is less than or equal to the arrayâs length. In this case, we are treating the string like an array. So, the index needs to be less than the length of the string.
How do I get the string length?
To get the length of the string, use the .Length property. Too easy.
Create a free account, or log in.
Gain access to free articles, game development tools, and game assets.