-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringFormat.cs
79 lines (70 loc) · 4.47 KB
/
StringFormat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;
/*
* Formatting may lengthen, reduce, or do nothing to the size of a string. It's more about presentation. Which might put it under Humanization?
*/
namespace MySQLCLRFunctions
{
public static class StringFormat
{
/***************************************************************************************************************************************************************************************************
*
* Append spaces on the right out to a fixed point. This is for displaying to a text output.
*
**************************************************************************************************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string RPad(string input, int length)
{
if (StringTest.IsNull(input)) return input;
return input.PadRight(length);
}
/***************************************************************************************************************************************************************************************************
*
* Prepend spaces on the left so that the text is right-justified. Could be zeroes, or X's.
*
**************************************************************************************************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string LPadC(string input, int length, char character)
{
if (StringTest.IsNull(input)) return input;
return input.PadLeft(length, character);
}
/***************************************************************************************************************************************************************************************************
*
* Append a specific character on the right out to a fixed point.
*
**************************************************************************************************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string RPadC(string input, int length, char character)
{
if (StringTest.IsNull(input)) return input;
return input.PadRight(length, character);
}
/***************************************************************************************************************************************************************************************************
*
* Prepend spaces on the left so that the text is right-justified.
*
**************************************************************************************************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string LPad(string input, int length)
{
if (StringTest.IsNull(input)) return input;
return input.PadLeft(length);
}
/***************************************************************************************************************************************************************************************************
*
* For name-style, this is like "UPPER" in SQL Server, as opposed to ToUpper in C#/.NET. I'm trying to avoid "To-" naming since I already use "Is-" naming and these are for SQL use.
*
**************************************************************************************************************************************************************************************/
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string Title(string input)
{
if (StringTest.IsNullOrWhiteSpaceOrEmpty(input)) return input;
return Regex.Replace(input.ToLower(), @"\b[a-z]\w+", (Match match) =>
{
string v = match.ToString();
return char.ToUpper(v[0]) + v.Substring(1);
});
}
}
}