EN
C# / .NET - add character to string
0
points
In this article, we would like to show you how to add character to string in C#.
Quick solution:
string text = "ABC";
char character = 'x';
string result = text + character;
Console.WriteLine(result); // ABCDx
or:
string text = "ABCD";
char character = 'x';
int index = 1;
string result = text.Insert(index, character.ToString());
Console.WriteLine(result); // AxBCD
1. Practical example using +
operator
In this example, we add a character at the end of the text
string using +
operator.
using System;
public class TestClass
{
public static void Main()
{
string text = "ABC";
char character = 'x';
string result = text + character;
Console.WriteLine(result);
}
}
Output:
ABCDx
2. Using String.Insert()
method
In this example, we use Insert()
method to add a character to the string at specific position (index).
using System;
public class TestClass
{
public static void Main()
{
string text = "ABCD";
char character = 'x';
int index = 1;
string result = text.Insert(index, character.ToString());
Console.WriteLine(result);
}
}
Output:
AxBCD