blob: 5699d27a079bdb11f847d9717c76a54c78afe937 (
plain)
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public static class StringExtensions
{
private static Regex titleRegEx;
/// <summary>
/// Initializes the <see cref="StringExtensions"/> class.
/// </summary>
static StringExtensions()
{
titleRegEx = new Regex(@"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
}
/// <summary>
/// Normal ToString conversion with null checking.
/// </summary>
/// <param name="obj">The object.</param>
/// <returns></returns>
public static String ToStringSafe(this object obj)
{
return obj != null ? obj.ToString() : String.Empty;
}
/// <summary>
/// Splits the string to lines.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static List<String> ToLines(this String str)
{
return str.Split(new[] { '\r', '\n' }).ToList();
}
/// <summary>
/// Formats the string to title style.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static String ToTitle(this String str)
{
return titleRegEx.Replace(str, " ");
}
}
|