Files
Articulate-ORM/Articulate/Extensions/StringExtension.cs
Ryan Palmer 72f1ccefca Start on QueryBuilder
Created:

- MySql Type Name Map
- Added String Extensions
2021-08-01 08:49:47 +10:00

28 lines
952 B
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Articulate.Extensions
{
public static class StringExtension
{
public static string AddUnderscoresToSentence(this string text, bool preserveAcronyms = false)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
StringBuilder newText = new StringBuilder(text.Length * 2);
newText.Append(text[0]);
for (int i = 1; i < text.Length; i++)
{
if (char.IsUpper(text[i]))
if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||
(preserveAcronyms && char.IsUpper(text[i - 1]) &&
i < text.Length - 1 && !char.IsUpper(text[i + 1])))
newText.Append('_');
newText.Append(text[i]);
}
return newText.ToString();
}
}
}