0

Does .Net have a nice way to convert something like this:

MyStringThatIsGreat

into this:

my-string-that-is-great

Ultimatly, I plan to call this in a powershell script, so I am hoping for a few commands that I can convert over to powershell (rather than a large bock of code).

(NOTE: if this a duplicate, I am sorry. I did a search and only saw stuff that went TO pascal case not FROM it to dash case.)

Edit: This is what I was able to coble together:

var pascalCase = "MyReally-CoolMFAString";
var dashCase = Regex.Replace(pascalCase, @"(?<!^)(?<!-)((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))", "-$1").ToLower();
Console.WriteLine(dashCase);

The output is:

my-really-cool-mfa-string

To make it work with powershell this is the command:

[System.Text.RegularExpressions.Regex]::Replace('MyReally-CoolMFAString', '(?<!^)(?<!-)((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))', '-$1').ToLower()

Note the single quotes. (Using double quotes you have to escape the $ symbol.)

Vaccano
  • 78,325
  • 149
  • 468
  • 850

1 Answers1

6

There are many ways to do this. Though regex might be a simple approach

var input = "MyStringThatIsGreat";

var result = Regex.Replace(input, "(?<!^)([A-Z])", "-$1").ToLower();

Console.WriteLine(result);

Or if you like for loops

var sb = new StringBuilder();
for (var i = 0; i < input.Length; i++)
   if (i != 0 && char.IsUpper(input[i]))
      sb.Append($"-{input[i]}");
   else
      sb.Append(input[i]);

Console.WriteLine(sb.ToString().ToLower());

Or Linq

var result = string
   .Concat(input
   .Select((x, i) => char.IsUpper(x) && i != 0 ? $"-{x}" : x.ToString()))
   .ToLower();

Output

my-string-that-is-great

Note : Both these examples are likely not very cultural aware.


Benchmarks

Job=.NET Core 5.0  Runtime=.NET Core 5.0

|        Method |     Mean |    Error |   StdDev |
|-------------- |---------:|---------:|---------:|
| RegexStandard | 988.6 ns | 19.26 ns | 31.65 ns |
| RegExCompiled | 566.5 ns | 11.08 ns | 16.24 ns |
| StringBuilder | 466.1 ns |  5.54 ns |  4.91 ns |
|        Concat | 792.4 ns | 15.09 ns | 16.77 ns |

Tests

[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class DumbTest
{
   private string _input = "MyStringThatIsGreat";
   private Regex compiled = new Regex("(?<!^)([A-Z])", RegexOptions.Compiled);

   [Benchmark]
   public string RegexStandard()
      => Regex.Replace(_input, "(?<!^)([A-Z])", "-$1").ToLower();

   [Benchmark]
   public string RegExCompiled()
      => compiled.Replace(_input, "-$1").ToLower();

   [Benchmark]
   public string StringBuilder()
   {
      var sb = new StringBuilder();
      for (var i = 0; i < _input.Length; i++)
         if (i != 0 && char.IsUpper(_input[i]))
            sb.Append($"-{_input[i]}");
         else
            sb.Append(_input[i]);
      return sb.ToString().ToLower();

   }

   [Benchmark]
   public string Concat() =>
      string
         .Concat(_input.Select((x, i) => char.IsUpper(x) && i != 0 ? $"-{x}" : x.ToString()))
         .ToLower();

}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141