"c# convert to snake case" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "c# convert to snake case" answered properly. Developers are finding an appropriate answer about c# convert to snake case related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like c# convert to snake case. Enter your desired code related query in the search bar and get every piece of information about C# code related question on c# convert to snake case. 

c# convert to snake case

By GeekInsideGeekInside on Apr 30, 2021
using System;
using System.Globalization;
using System.Text;

namespace Extensions
{
    public static class StringExtensions
    {
        public static string ToSnakeCase(this string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return text;
            }

            var builder = new StringBuilder(text.Length + Math.Min(2, text.Length / 5));
            var previousCategory = default(UnicodeCategory?);

            for (var currentIndex = 0; currentIndex < text.Length; currentIndex++)
            {
                var currentChar = text[currentIndex];
                if (currentChar == '_')
                {
                    builder.Append('_');
                    previousCategory = null;
                    continue;
                }

                var currentCategory = char.GetUnicodeCategory(currentChar);
                switch (currentCategory)
                {
                    case UnicodeCategory.UppercaseLetter:
                    case UnicodeCategory.TitlecaseLetter:
                        if (previousCategory == UnicodeCategory.SpaceSeparator ||
                            previousCategory == UnicodeCategory.LowercaseLetter ||
                            previousCategory != UnicodeCategory.DecimalDigitNumber &&
                            previousCategory != null &&
                            currentIndex > 0 &&
                            currentIndex + 1 < text.Length &&
                            char.IsLower(text[currentIndex + 1]))
                        {
                            builder.Append('_');
                        }

                        currentChar = char.ToLower(currentChar, CultureInfo.InvariantCulture);
                        break;

                    case UnicodeCategory.LowercaseLetter:
                    case UnicodeCategory.DecimalDigitNumber:
                        if (previousCategory == UnicodeCategory.SpaceSeparator)
                        {
                            builder.Append('_');
                        }
                        break;

                    default:
                        if (previousCategory != null)
                        {
                            previousCategory = UnicodeCategory.SpaceSeparator;
                        }
                        continue;
                }

                builder.Append(currentChar);
                previousCategory = currentCategory;
            }

            return builder.ToString();
        }
    }
}

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on c# convert to snake case can get a collection of related answers to their query. Programmers need to enter their query on c# convert to snake case related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about c# convert to snake case for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of c# convert to snake case while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "c# convert to snake case". Visit this developer's friendly online web community, CodeProZone, and get your queries like c# convert to snake case resolved professionally and stay updated to the latest C# updates. 

C# answers related to "c# convert to snake case"

View All C# queries

C# queries related to "c# convert to snake case"

c# convert to snake case c# string equals ignore case c# switch case set value c# To Pascal Case csharp switch case "Control cannot fall out of switch from final case label ('default:') c# how to pass object as test case in nunit c# c# reverse a string and case switch case java switch/case with numeric range unity how to convert mouse screen position to world position c# convert Unix time in seconds to datetime convert string array to int C# how to convert int to string unity c# convert int to string in linq query c# convert array to list Unity C# convert string to boolean c# convert datetime to user timezone c# convert uint to int C# stack overflow c# convert string to int console.readline(convert.toint32) c# c# convert address to int Convert C# Class to xml wth xsd.exe asp net mvc convert ienumerable to selectlistitem c# convert to absolute value F# convert generic.List to list c# convert queue to list convert relative path to physical path c# c# convert string to enum value how to convert int to char in c# convert numbers to words C# vb.net convert int32 into boolean array stack overflow convert xml string to file c# how to convert c# string to pdf convert text to ssml function convert list of tuples to dictionary c# cannot implicitly convert type 'system.threading.tasks.task string ' to 'string' c# convert word files to plain text c# c# convert excel column index to letter how to convert a key state to a letter in monogame c# convert linq jValue to int convert dictionary to object c# convert table to Csharp class linq convert list to another list c# convert enumb to int array how to convert from hexadecimal to binary in c# convert to int c# c# convert ad objectguid to string C# convert random numbers in textBox to currency convert foreach to linq c# vbnet programatically convert type to db type c# program for convert kg to pound convert arraylist to array int convert Integer arraylist to array java convert arraylist of integers to array primitive Convert string int Linq

Browse Other Code Languages

CodeProZone