-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringExtensionMethod.cs
More file actions
48 lines (42 loc) · 1.57 KB
/
StringExtensionMethod.cs
File metadata and controls
48 lines (42 loc) · 1.57 KB
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
using System;
using System.Linq;
namespace AdjustStringProperty
{
public static class StringExtensionMethod
{
/// <summary>
///Replace Multiple Spaces With Single
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static string ReplaceMultipleSpacesWithSingle(this string input)
{
return string.Join(" ", input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
private static string ReplaceString(this string obj)
{
return obj.Replace((char)1603, (char)1705)
.Replace((char)1609, (char)1740)
.Replace((char)1610, (char)1740);
}
public static TModel AdjustStringProperties<TModel>(this TModel model)
{
if (model == null) return model;
var stringProperties = typeof(TModel).GetProperties()
.Where(y => y.PropertyType == typeof(string) && y.CanWrite);
foreach (var stringProperty in stringProperties)
{
var currentValue = (string)stringProperty.GetValue(model, null);
if (!string.IsNullOrEmpty(currentValue))
{
var newValue = currentValue
.Trim()
.ReplaceMultipleSpacesWithSingle()
.ReplaceString();
stringProperty.SetValue(model, newValue, null);
}
}
return model;
}
}
}