Skip to content

Commit 9bdeb94

Browse files
committed
resolved #197
1 parent 46d5815 commit 9bdeb94

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/CatLib.Core.Tests/Support/Util/StrTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,25 @@ public void TestRandom()
264264
Assert.AreEqual(16, str.Length);
265265
}
266266

267+
[TestMethod]
268+
public void TestStrSpace()
269+
{
270+
Assert.AreEqual(" ", Str.Space);
271+
}
272+
267273
[TestMethod]
268274
public void TestTruncate()
269275
{
270276
var str = Str.Truncate("hello world , the sun is shine", 11);
271277
Assert.AreEqual("hello wo...", str);
272278

273-
str = Str.Truncate("hello world , the sun is shine", 11, " ");
279+
str = Str.Truncate("hello world , the sun is shine", 11, Str.Space);
274280
Assert.AreEqual("hello...", str);
275281

276-
str = Str.Truncate("hello world , the sun is shine", 15, " ");
282+
str = Str.Truncate("hello world , the sun is shine", 15, Str.Space);
277283
Assert.AreEqual("hello world...", str);
278284

279-
str = Str.Truncate("hello world sun sname", 15, " ");
285+
str = Str.Truncate("hello world sun sname", 15, Str.Space);
280286
Assert.AreEqual("hello world..." , str);
281287

282288
var regex = new Regex("orl");

src/CatLib.Core/Support/Util/Str.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public enum PadTypes
4141
Right
4242
}
4343

44+
/// <summary>
45+
/// 空格字符串
46+
/// </summary>
47+
public const string Space = " ";
48+
4449
/// <summary>
4550
/// 获取字符串所表达的函数名
4651
/// </summary>
@@ -305,8 +310,8 @@ public static string Pad(string str, int length, string padStr = null, PadTypes
305310
leftPadding = needPadding;
306311
}
307312

308-
padStr = padStr ?? " ";
309-
padStr = padStr.Length <= 0 ? " " : padStr;
313+
padStr = padStr ?? Space;
314+
padStr = padStr.Length <= 0 ? Space : padStr;
310315

311316
var leftPadCount = leftPadding / padStr.Length + (leftPadding % padStr.Length == 0 ? 0 : 1);
312317
var rightPadCount = rightPadding / padStr.Length + (rightPadding % padStr.Length == 0 ? 0 : 1);
@@ -438,7 +443,7 @@ public static string Random(int length = 16, int? seed = null)
438443

439444
/// <summary>
440445
/// 如果长度超过给定的最大字符串长度,则截断字符串。 截断的字符串的最后一个字符将替换为缺省字符串
441-
/// <para>eg: Str.Truncate("hello world , the sun is shine",15," ") => hello world...</para>
446+
/// <para>eg: Str.Truncate("hello world , the sun is shine", 15, Str.Space) => hello world...</para>
442447
/// </summary>
443448
/// <param name="str">要截断的字符串</param>
444449
/// <param name="length">截断长度(含缺省字符长度)</param>

0 commit comments

Comments
 (0)