新增字符串扩展方法SubstringEx和Split方法。

This commit is contained in:
falcon 2022-12-09 11:17:04 +08:00
parent 2488b15532
commit 4fb6934cea
2 changed files with 140 additions and 31 deletions

View File

@ -1,4 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySqlX.XDevAPI.Common;
using System;
using System.Linq;
namespace Falcon.SugarApi.Test
{
@ -56,5 +59,59 @@ namespace Falcon.SugarApi.Test
r = str.ToDefault("abc");
Assert.AreEqual("123",r);
}
/// <summary>
/// 检索子字符串测试
/// </summary>
[TestMethod]
public void TestSubstringEx() {
var str = "abcd33";
string r;
r = str.SubstringEx(str.Length + 1,1);
Assert.IsTrue(r == "");
r = str.SubstringEx(0,0);
Assert.IsTrue(r == "");
r = str.SubstringEx(3,-1);
Assert.IsTrue(r == "c");
r = str.SubstringEx(3,10);
Assert.IsTrue(r == "d33");
r = str.SubstringEx(-1,2);
Assert.IsTrue(r == "a");
r = str.SubstringEx(1,2);
Assert.IsTrue(r == "bc");
}
/// <summary>
/// 按一定长度分割字符串测试
/// </summary>
[TestMethod]
public void TestSplitByLength() {
string str;
string[] result;
str = "";
Assert.ThrowsException<ArgumentNullException>(() => { result = str.Split(5).ToArray(); });
str = "abc";
Assert.ThrowsException<Exception>(() => { result = str.Split(0).ToArray(); });
result = str.Split(1).ToArray();
Assert.IsTrue(result.Length == 3);
Assert.IsTrue(result[0] == "a");
Assert.IsTrue(result[1] == "b");
Assert.IsTrue(result[2] == "c");
result = str.Split(2).ToArray();
Assert.IsTrue(result.Length == 2);
Assert.IsTrue(result[0] == "ab");
Assert.IsTrue(result[1] == "c");
result = str.Split(3).ToArray();
Assert.IsTrue(result.Length == 1);
Assert.IsTrue(result[0] == "abc");
result = str.Split(4).ToArray();
Assert.IsTrue(result.Length == 1);
Assert.IsTrue(result[0] == "abc");
}
}
}

View File

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection.Metadata;
namespace Falcon.SugarApi
{
@ -66,8 +69,7 @@ namespace Falcon.SugarApi
try {
dt = str.ToDateTime(dateTimeFormat,culture);
return true;
}
catch (Exception) {
} catch(Exception) {
dt = default;
return false;
}
@ -89,5 +91,55 @@ namespace Falcon.SugarApi
}
return str.TryToDateTime(dateFormat,out var _);
}
/// <summary>
/// 获取子字符串
/// <para>如果start大于字符串长度或者length等于0则返回空字符串。</para>
/// <para>如果length小于0则返回start前面长度的字符串。</para>
/// <para>如果字符串不满足需要的长度要求则返回实际长度字符串。</para>
/// <para>如果start小于0则返回从0开始的length+start长度字符串。</para>
/// </summary>
/// <param name="str">要检索的字符串</param>
/// <param name="start">开始位置</param>
/// <param name="length">获取长度</param>
/// <returns>子字符串</returns>
/// <exception cref="Exception">获取长度不满足要求</exception>
public static string SubstringEx(this string str,int start,int length) {
if(start > str.Length || length == 0) {
return "";
}
if(length < 0) {
start = start + length;
length = -length;
}
if(start < 0) {
length += start;
start = 0;
}
if(start + length > str.Length) {
return str.Substring(start);
}
return str.Substring(start,length);
}
/// <summary>
/// 按照固定长度分割字符串
/// </summary>
/// <param name="str">要分割的字符串</param>
/// <param name="length">分割长度</param>
/// <returns>分割后的字符串枚举</returns>
public static IEnumerable<string> Split(this string str,int length) {
if(length <= 0) {
throw new Exception("分割长度必须大于0");
}
if(str.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(str));
}
int i = 0;
while(str.Length > i) {
yield return str.SubstringEx(i,length);
i += length;
}
}
}
}