Skip to content

Commit aa54a9d

Browse files
committed
Fixed aspnetboilerplate#462. Also added some useful string extension methods.
1 parent 7e16e4b commit aa54a9d

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

src/Abp/AbpConsts.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public static class AbpConsts
88
/// <summary>
99
/// Current version of the ABP.
1010
/// </summary>
11-
public const string CurrentVersion = "0.5.12.1";
11+
public const string CurrentVersion = "0.5.13.0";
1212
}
1313
}

src/Abp/Extensions/StringExtensions.cs

+40-7
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ public static bool IsNullOrWhiteSpace(this string str)
115115
/// <summary>
116116
/// Gets a substring of a string from beginning of the string.
117117
/// </summary>
118-
/// <param name="str"></param>
119-
/// <param name="len"></param>
120-
/// <returns></returns>
121118
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
122119
/// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
123120
public static string Left(this string str, int len)
@@ -135,6 +132,14 @@ public static string Left(this string str, int len)
135132
return str.Substring(0, len);
136133
}
137134

135+
/// <summary>
136+
/// Converts line endings in the string to <see cref="Environment.NewLine"/>.
137+
/// </summary>
138+
public static string NormalizeLineEndings(this string str)
139+
{
140+
return str.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine);
141+
}
142+
138143
/// <summary>
139144
/// Gets index of nth occurence of a char in a string.
140145
/// </summary>
@@ -168,9 +173,6 @@ public static int NthIndexOf(this string str, char c, int n)
168173
/// <summary>
169174
/// Gets a substring of a string from end of the string.
170175
/// </summary>
171-
/// <param name="str"></param>
172-
/// <param name="len"></param>
173-
/// <returns></returns>
174176
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
175177
/// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
176178
public static string Right(this string str, int len)
@@ -188,6 +190,37 @@ public static string Right(this string str, int len)
188190
return str.Substring(str.Length - len, len);
189191
}
190192

193+
/// <summary>
194+
/// Uses string.Split method to split given string by given separator.
195+
/// </summary>
196+
public static string[] Split(this string str, string separator)
197+
{
198+
return str.Split(new[] { separator }, StringSplitOptions.None);
199+
}
200+
201+
/// <summary>
202+
/// Uses string.Split method to split given string by given separator.
203+
/// </summary>
204+
public static string[] Split(this string str, string separator, StringSplitOptions options)
205+
{
206+
return str.Split(new[] { separator }, options);
207+
}
208+
209+
/// <summary>
210+
/// Uses string.Split method to split given string by <see cref="Environment.NewLine"/>.
211+
/// </summary>
212+
public static string[] SplitToLines(this string str)
213+
{
214+
return str.Split(Environment.NewLine);
215+
}
216+
217+
/// <summary>
218+
/// Uses string.Split method to split given string by <see cref="Environment.NewLine"/>.
219+
/// </summary>
220+
public static string[] SplitToLines(this string str, StringSplitOptions options)
221+
{
222+
return str.Split(Environment.NewLine, options);
223+
}
191224

192225
/// <summary>
193226
/// Converts PascalCase string to camelCase string.
@@ -333,7 +366,7 @@ public static string TruncateWithPostfix(this string str, int maxLength, string
333366
{
334367
return string.Empty;
335368
}
336-
369+
337370
if (str.Length <= maxLength)
338371
{
339372
return str;

src/Abp/Localization/Dictionaries/Xml/XmlLocalizationDictionary.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static XmlLocalizationDictionary BuildFomXmlString(string xmlString)
8484
dublicateNames.Add(name);
8585
}
8686

87-
dictionary[name] = node.GetAttributeValueOrNull("value") ?? node.InnerText;
87+
dictionary[name] = (node.GetAttributeValueOrNull("value") ?? node.InnerText).NormalizeLineEndings();
8888
}
8989
}
9090

src/Tests/Abp.Tests/Extensions/StringExtensions_Tests.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Globalization;
3+
using System.Linq;
34
using Abp.Extensions;
45
using Shouldly;
56
using Xunit;
@@ -79,6 +80,15 @@ public void Left_Test()
7980
str.Left(str.Length).ShouldBe(str);
8081
}
8182

83+
[Fact]
84+
public void NormalizeLineEndings_Test()
85+
{
86+
const string str = "This\r\n is a\r test \n string";
87+
var normalized = str.NormalizeLineEndings();
88+
var lines = normalized.SplitToLines();
89+
lines.Length.ShouldBe(4);
90+
}
91+
8292
[Fact]
8393
public void NthIndexOf_Test()
8494
{
@@ -121,7 +131,7 @@ public void TruncateWithPostFix_Test()
121131
str.TruncateWithPostfix(12, "~").ShouldBe("This is a t~");
122132
str.TruncateWithPostfix(0, "~").ShouldBe("");
123133
str.TruncateWithPostfix(100, "~").ShouldBe(str);
124-
134+
125135
nullValue.TruncateWithPostfix(5, "~").ShouldBe(null);
126136
}
127137

0 commit comments

Comments
 (0)