Languages
[Edit]
EN

C# / .NET - replace first character in string (range operator ..)

0 points
Created by:
Zeeshan-Peel
850

In this article, we would like to show you how to replace the first character in a string in C# / .NET.

Note: range operator .. was introduced in C# 8.0.

Quick solution:

 

1. Practical examples

Edit

1.1 With + and range operator

Edit

In this example, we will take the text string without the first character and add it at the end of the replacement.

1.2 With string.Concat() method and range operator

Edit

This approach is equivalent to the above one. Instead of + operator we concatenate the strings using string.Concat() method.

2. Practical example using string Regex.Replace() with regex pattern

Edit

In this example, we use Regex.Replace() with "^." regex to replace the first character in the text string.

Regex explanation:

  • ^ - matches the beginning of the string,
  • . - matches any character except linebreaks.
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join