Mohanapriya R Mohanapriya R
Updated date Aug 27, 2024
In this article, we will learn how to convert strings to kebab case in C#.
  • 1.4k
  • 0
  • 0

What is Kebab Case:

Kebab case is a naming convention where words are separated by hyphens ("-"). It is commonly used in URLs, CSS classes, and other contexts where spaces are not allowed. For example, the string "Convert This String" would become "convert-this-string" in kebab case.

How to Convert Strings to Kebab Case?

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string inputString = "Convert This String";
        string kebabCaseString = ConvertToKebabCase(inputString);

        Console.WriteLine("Original String: " + inputString);
        Console.WriteLine("Kebab Case: " + kebabCaseString);
    }

    static string ConvertToKebabCase(string input)
    {
        // Replace spaces with hyphens and convert to lowercase
        return Regex.Replace(input, @"\s+", "-").ToLower();
    }
}

We declared a sample string inputString with the value "Convert This String." The ConvertToKebabCase method takes a string as input and uses a regular expression to replace spaces with hyphens and convert the entire string to lowercase.

Output:

Original String: Convert This String
Kebab Case: convert-this-string

Comments (0)

There are no comments. Be the first to comment!!!