Home
Search
String Chars (Get Char at Index)Access characters in strings with the indexer built into the string type.
C#
This page was last reviewed on Dec 15, 2022.
String chars. Strings contain characters. These char values can be accessed with an indexer expression in the C# language. We can test and loop over chars.
Char notes. We can store these chars in separate variables. We cannot mutate (assign) chars in a string. But we can use them to build up new strings.
Loop Over String Chars
An example. We access a character by using the square brackets. To get the first character, you can specify variable[0]. To get the last character, you can subtract one from the length.
Tip This is because the final offset in a string is always one less than its length, when length is one or more.
String Length
Console With Console, we print some text and the character values to the terminal window.
Console
using System; class Program { static void Main() { // Get values from this string. string value = "hat"; char first = value[0]; char second = value[1]; char last = value[value.Length - 1]; // Write chars. Console.WriteLine("FIRST: {0}", first); Console.WriteLine("SECOND: {0}", second); Console.WriteLine("LAST: {0}", last); } }
FIRST: h SECOND: a LAST: t
Example 2. When you have a string that is null, it does not point to any object data, and you cannot use the indexer on it. Therefore, the character accesses will not succeed.
And With empty strings, the Length is zero, so there are no available offsets for you to access in the string.
Here The string.IsNullOrEmpty static method checks for strings that cannot have their characters accessed.
IsNullOrEmpty, IsNullOrWhiteSpace
Tip IsNullOrEmpty allows the enclosed block to execute only when there is one or more characters.
using System; class Program { static void Main() { // Array of string values. string[] values = { "Dot Net Perls", "D", "", "Sam" }; // Loop over string values. foreach (string value in values) { // Display the string. Console.WriteLine("--- '{0}' ---", value); // We can't get chars from null or empty strings. if (!string.IsNullOrEmpty(value)) { char first = value[0]; char last = value[value.Length - 1]; Console.Write("First char: "); Console.WriteLine(first); Console.Write("Last char: "); Console.WriteLine(last); } } } }
--- 'Dot Net Perls' --- First char: D Last char: s --- 'D' --- First char: D Last char: D --- '' --- --- 'Sam' --- First char: S Last char: m
Internals. An indexer is a property. It allows you to access the object's data using the square brackets, such as in variable[0].
Indexer
And In the .NET Framework, the chars are accessed in an internal method, not in managed code.
Char indexer:
.property instance char Chars { .get instance char System.String::get_Chars(int32) }
A discussion. Often we need to loop over the individual characters in a string. You can do this with the foreach loop, or with the for iterative statement using the char accessors.
Foreach
Char
Also It is almost always a mistake to access a specific character in a string with the Substring method.
Instead Accessing the char directly is much faster and results in no allocations.
Info I changed an important algorithm to use the char indexer instead of 1-character substrings. The program became 75% faster.
Levenshtein
A summary. Characters in strings can be accessed, but not modified. We saw some issues relating to empty and null strings and trying to access their characters.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2023 Sam Allen.