Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# To Pascal Case

public static string PascalCase(this string word)
{
    return string.Join("" , word.Split('_')
                 .Select(w => w.Trim())
                 .Where(w => w.Length > 0)
                 .Select(w => w.Substring(0,1).ToUpper() + w.Substring(1).ToLower()));
}
Comment

c# To Pascal Case

using System;
using System.Linq;
using System.Text.RegularExpressions;

public string ToPascalCase(string original)
{
    Regex invalidCharsRgx = new Regex("[^_a-zA-Z0-9]");
    Regex whiteSpace = new Regex(@"(?<=s)");
    Regex startsWithLowerCaseChar = new Regex("^[a-z]");
    Regex firstCharFollowedByUpperCasesOnly = new Regex("(?<=[A-Z])[A-Z0-9]+$");
    Regex lowerCaseNextToNumber = new Regex("(?<=[0-9])[a-z]");
    Regex upperCaseInside = new Regex("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))");

    // replace white spaces with undescore, then replace all invalid chars with empty string
    var pascalCase = invalidCharsRgx.Replace(whiteSpace.Replace(original, "_"), string.Empty)
        // split by underscores
        .Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)
        // set first letter to uppercase
        .Select(w => startsWithLowerCaseChar.Replace(w, m => m.Value.ToUpper()))
        // replace second and all following upper case letters to lower if there is no next lower (ABC -> Abc)
        .Select(w => firstCharFollowedByUpperCasesOnly.Replace(w, m => m.Value.ToLower()))
        // set upper case the first lower case following a number (Ab9cd -> Ab9Cd)
        .Select(w => lowerCaseNextToNumber.Replace(w, m => m.Value.ToUpper()))
        // lower second and next upper case letters except the last if it follows by any lower (ABcDEf -> AbcDef)
        .Select(w => upperCaseInside.Replace(w, m => m.Value.ToLower()));

    return string.Concat(pascalCase);
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: constructor in c# 
Csharp :: how to add event function from code in wpf 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: c# remove invalid directory characters 
Csharp :: c# console delete last character 
Csharp :: C# The request was aborted: Could not create SSL/TLS secure 
Csharp :: c# caractère cacher mot de passe 
Csharp :: go right unity 
Csharp :: how to do that a objetct moves in c# 
Csharp :: What are logic gates? 
Csharp :: program.cs entity framework 
Csharp :: unity inspector draw line 
Csharp :: save position unity 
Csharp :: how to detect ajax request in asp.net core 
Csharp :: c# create a dummy class 
Csharp :: int to char c# 
Csharp :: administrative priviledge in c# 
Csharp :: dictionaries in unity 
Csharp :: unity how to make gamemanager instance 
Csharp :: c# press ctrl and alt 
Csharp :: vb.net delete folder if exists 
Csharp :: vb.net windows version check 
Csharp :: c# winforms datagridview bind to list 
Csharp :: serilog asp.net 5 
Csharp :: select from list where not in other list c# 
Csharp :: dotnet add package 
Csharp :: c# split string 
Csharp :: c# define array 
Csharp :: how to configure visual studio for unity 
Csharp :: c# double 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =