Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# convert to snake case

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();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: wpf rounded image 
Csharp :: c# unity destroy first child object 
Csharp :: how to log out of unity asset store 
Csharp :: How to Create Hint, PlaceHolder Text, Watermark In a TextBox vb.net 
Csharp :: unity check if audio playing 
Csharp :: after each vue router 
Csharp :: c# shuffle string array 
Csharp :: animations for pause menu 
Csharp :: unity how to end a game with esc 
Csharp :: oncollisionenter 
Csharp :: unity how to reorder a list 
Csharp :: string format comma c# 
Csharp :: textbox only numbers c# 
Csharp :: iterate through xpdictionary devexpress 
Csharp :: convert object to xml c# example code 
Csharp :: checkbox value unchecked after return view model 
Csharp :: .net core authorizationhandlercontext 
Csharp :: delete all dir content c# 
Csharp :: unity spawn button 
Csharp :: c# datetime now timestamp 
Csharp :: unity hub black screen 
Csharp :: get distinct from datatable c# 
Csharp :: c# int to hex 
Csharp :: timer in c# 
Csharp :: video gets pixelated unity 
Csharp :: c# inline a function 
Csharp :: c# list remove duplicate items 
Csharp :: tinyint in c# 
Csharp :: round float c# 
Csharp :: how to loop an animation in unity 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =