1 using System.Globalization;
2 using Microsoft.Xna.Framework;
5 using System.Collections.Generic;
9 public static class StringFormatter
11 public static string Replace(
this string s,
string replacement, Func<char, bool> predicate)
13 var newString =
new string[s.Length];
14 for (
int i = 0; i < s.Length; i++)
17 string newLetter = letter.ToString();
18 if (predicate(letter))
20 newLetter = replacement;
22 newString[i] = newLetter;
24 return new string(newString.SelectMany(str => str.ToCharArray()).ToArray());
27 public static string Remove(
this string s,
string substring, StringComparison comparisonType = StringComparison.Ordinal)
29 return s.Replace(substring,
string.Empty, comparisonType);
32 public static string Remove(
this string s, Func<char, bool> predicate)
34 return new string(s.ToCharArray().Where(c => !predicate(c)).ToArray());
37 public static string RemoveWhitespace(
this string s)
39 return s.Remove(c =>
char.IsWhiteSpace(c));
42 public static string FormatSingleDecimal(
this float value)
44 return value.ToString(
"F1", CultureInfo.InvariantCulture);
47 public static string FormatDoubleDecimal(
this float value)
49 return value.ToString(
"F2", CultureInfo.InvariantCulture);
52 public static string FormatZeroDecimal(
this float value)
54 return value.ToString(
"F0", CultureInfo.InvariantCulture);
57 public static string Format(
this float value,
int decimalCount)
59 return value.ToString($
"F{decimalCount}", CultureInfo.InvariantCulture);
62 public static string FormatSingleDecimal(
this Vector2 value)
64 return $
"({value.X.FormatSingleDecimal()}, {value.Y.FormatSingleDecimal()})";
67 public static string FormatSingleDecimal(
this Vector3 value)
69 return $
"({value.X.FormatSingleDecimal()}, {value.Y.FormatSingleDecimal()}, {value.Z.FormatSingleDecimal()})";
72 public static string FormatSingleDecimal(
this Vector4 value)
74 return $
"({value.X.FormatSingleDecimal()}, {value.Y.FormatSingleDecimal()}, {value.Z.FormatSingleDecimal()}, {value.W.FormatSingleDecimal()})";
77 public static string FormatDoubleDecimal(
this Vector2 value)
79 return $
"({value.X.FormatDoubleDecimal()}, {value.Y.FormatDoubleDecimal()})";
82 public static string FormatZeroDecimal(
this Vector2 value)
84 return $
"({value.X.FormatZeroDecimal()}, {value.Y.FormatZeroDecimal()})";
87 public static string Format(
this Vector2 value,
int decimalCount)
89 return $
"({value.X.Format(decimalCount)}, {value.Y.Format(decimalCount)})";
95 public static string CapitaliseFirstInvariant(
this string s)
97 if (
string.IsNullOrEmpty(s)) {
return string.Empty; }
98 return s.Substring(0, 1).ToUpperInvariant() + s.Substring(1, s.Length - 1).ToLowerInvariant();
104 public static string FormatCamelCaseWithSpaces(
this string str)
106 return new string(InsertSpacesBeforeCaps(str).ToArray());
107 IEnumerable<char> InsertSpacesBeforeCaps(IEnumerable<char> input)
110 int lastChar = input.Count() - 1;
111 foreach (
char c
in input)
113 if (
char.IsUpper(c) && i > 0)
124 public static ICollection<string> ParseCommaSeparatedStringToCollection(
string input, ICollection<string> texts =
null,
bool convertToLowerInvariant =
true)
128 texts =
new HashSet<string>();
134 if (!
string.IsNullOrWhiteSpace(input))
136 foreach (
string value
in input.Split(
','))
138 if (
string.IsNullOrWhiteSpace(value)) {
continue; }
139 if (convertToLowerInvariant)
141 texts.Add(value.ToLowerInvariant());
152 public static ICollection<string> ParseSeparatedStringToCollection(
string input,
string[] separators, ICollection<string> texts =
null,
bool convertToLowerInvariant =
true)
156 texts =
new HashSet<string>();
162 if (!
string.IsNullOrWhiteSpace(input))
164 foreach (
string value
in input.Split(separators, StringSplitOptions.RemoveEmptyEntries))
166 if (convertToLowerInvariant)
168 texts.Add(value.ToLowerInvariant());