Skip to content

Commit c8f8bf9

Browse files
committed
resolved #198
1 parent 9bdeb94 commit c8f8bf9

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,35 +151,35 @@ public void TestReverse()
151151
[TestMethod]
152152
public void TestPad()
153153
{
154-
var result = Str.Pad("hello", 10, "worldtest", Str.PadTypes.Both);
154+
var result = Str.Pad(10, "hello", "worldtest", Str.PadTypes.Both);
155155
Assert.AreEqual("wohellowor", result);
156156

157-
result = Str.Pad("hello", 10, "worldtest");
157+
result = Str.Pad(10, "hello", "worldtest");
158158
Assert.AreEqual("helloworld", result);
159159

160-
result = Str.Pad("hello", 10, "worldtest" , Str.PadTypes.Left);
160+
result = Str.Pad(10, "hello", "worldtest" , Str.PadTypes.Left);
161161
Assert.AreEqual("worldhello", result);
162162

163-
result = Str.Pad("hello", 3, "worldtest", Str.PadTypes.Left);
163+
result = Str.Pad(3, "hello", "worldtest", Str.PadTypes.Left);
164164
Assert.AreEqual("hello", result);
165165

166-
result = Str.Pad("hello", 10, null, Str.PadTypes.Left);
166+
result = Str.Pad(10, "hello", null, Str.PadTypes.Left);
167167
Assert.AreEqual(" hello", result);
168168

169-
result = Str.Pad("hello", 10, string.Empty, Str.PadTypes.Left);
169+
result = Str.Pad(10, "hello", string.Empty, Str.PadTypes.Left);
170170
Assert.AreEqual(" hello", result);
171171
}
172172

173173
[TestMethod]
174174
public void TestEmptyStrPad()
175175
{
176-
var result = Str.Pad(string.Empty, 10, "wor", Str.PadTypes.Left);
176+
var result = Str.Pad(10, string.Empty, "wor", Str.PadTypes.Left);
177177
Assert.AreEqual("worworworw", result);
178178

179-
result = Str.Pad(string.Empty, 10, "wor", Str.PadTypes.Both);
179+
result = Str.Pad(10, string.Empty, "wor", Str.PadTypes.Both);
180180
Assert.AreEqual("worwoworwo", result);
181181

182-
result = Str.Pad(string.Empty, 10, "wor");
182+
result = Str.Pad(10, null, "wor");
183183
Assert.AreEqual("worworworw", result);
184184
}
185185

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,32 @@ public static string Reverse(string str)
283283
/// <para><see cref="PadTypes.Right"/>填充字符串的右侧。默认。</para>
284284
/// </param>
285285
/// <returns>被填充的字符串</returns>
286+
[Obsolete("The overload method wile be remove in 2.0 version.")]
286287
public static string Pad(string str, int length, string padStr = null, PadTypes type = PadTypes.Right)
287288
{
288-
Guard.Requires<ArgumentNullException>(str != null);
289+
return Pad(length, str, padStr, type);
290+
}
291+
292+
/// <summary>
293+
/// 把字符串填充为新的长度。
294+
/// </summary>
295+
/// <param name="length">规定新的字符串长度。如果该值小于字符串的原始长度,则不进行任何操作。</param>
296+
/// <param name="str">原始字符串,如果为null则为空值</param>
297+
/// <param name="padStr">
298+
/// 规定供填充使用的字符串。默认是空白。
299+
/// <para>如果传入的字符串长度小于等于0那么会使用空白代替。</para>
300+
/// <para>注释:空白不是空字符串</para>
301+
/// </param>
302+
/// <param name="type">
303+
/// 规定填充字符串的哪边。
304+
/// <para><see cref="PadTypes.Both"/>填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。</para>
305+
/// <para><see cref="PadTypes.Left"/>填充字符串的左侧。</para>
306+
/// <para><see cref="PadTypes.Right"/>填充字符串的右侧。默认。</para>
307+
/// </param>
308+
/// <returns>被填充的字符串</returns>
309+
public static string Pad(int length, string str = null, string padStr = null, PadTypes type = PadTypes.Right)
310+
{
311+
str = str ?? string.Empty;
289312

290313
var needPadding = length - str.Length;
291314
if (needPadding <= 0)

0 commit comments

Comments
 (0)