Home > Backend Development > C++ > How Can I Capitalize the First Letter of a String in C# with Optimal Performance?

How Can I Capitalize the First Letter of a String in C# with Optimal Performance?

Susan Sarandon
Release: 2025-01-16 13:44:00
Original
541 people have browsed it

How Can I Capitalize the First Letter of a String in C# with Optimal Performance?

Efficiently capitalize the first letter of C# string

Introduction

Capitalizing the first letter of a string is a common task in many programming applications. In order to optimize performance, it is crucial to choose an efficient method. This article explores several ways to implement capitalization in C#, and focuses on analyzing their performance differences.

Code Example

C# 8, .NET Core 3.0 or .NET Standard 2.1

<code class="language-csharp">public static string FirstCharToUpper(this string input) =>
    input switch
    {
        null => throw new ArgumentNullException(nameof(input)),
        "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
        _ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1))
    };</code>
Copy after login

C# 8

<code class="language-csharp">public static string FirstCharToUpper(this string input) =>
    input switch
    {
        null => throw new ArgumentNullException(nameof(input)),
        "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
        _ => input[0].ToString().ToUpper() + input.Substring(1)
    };</code>
Copy after login

C# 7

<code class="language-csharp">public static string FirstCharToUpper(this string input)
{
    switch (input)
    {
        case null: throw new ArgumentNullException(nameof(input));
        case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
        default: return input[0].ToString().ToUpper() + input.Substring(1);
    }
}</code>
Copy after login

Old version (not recommended, poor performance)

<code class="language-csharp">public static string FirstCharToUpper(string input)
{
    if (String.IsNullOrEmpty(input))
        throw new ArgumentException("ARGH!");
    return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}</code>
Copy after login
<code class="language-csharp">public static string FirstCharToUpper(string input)
{
    if (String.IsNullOrEmpty(input))
        throw new ArgumentException("ARGH!");
    return input.First().ToString().ToUpper() + input.Substring(1);
}</code>
Copy after login

Performance Considerations

Among these code snippets, the method with the least memory allocation and the most efficient string manipulation has the best performance. The first solution uses ReadonlySpan<char> from .NET Core 3.0 or .NET Standard 2.1, which provides superior performance compared to other methods.

The above is the detailed content of How Can I Capitalize the First Letter of a String in C# with Optimal Performance?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template