2022-03-22 16:53:24 +08:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2022-12-09 11:17:04 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2022-03-22 16:53:24 +08:00
|
|
|
|
|
|
|
|
|
namespace Falcon.SugarApi.Test
|
|
|
|
|
{
|
2022-10-21 09:13:44 +08:00
|
|
|
|
|
2022-03-30 17:08:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符串扩展方法测试
|
|
|
|
|
/// </summary>
|
2022-03-22 16:53:24 +08:00
|
|
|
|
[TestClass]
|
|
|
|
|
public class StringExtendTest
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestSplit() {
|
|
|
|
|
var str = $"a,b.c;d,e。f;g";
|
|
|
|
|
var strArr = str.SplitStr();
|
2022-12-09 11:17:04 +08:00
|
|
|
|
Assert.AreEqual(7,strArr.Length,"分割后长度应该为7");
|
2022-03-29 16:14:07 +08:00
|
|
|
|
str = "a,,, ,b ,c c,";
|
|
|
|
|
strArr = str.SplitStr();
|
2022-12-09 11:17:04 +08:00
|
|
|
|
Assert.AreEqual(3,strArr.Length,"分割后长度应该为3");
|
|
|
|
|
Assert.AreEqual("a",strArr[0]);
|
|
|
|
|
Assert.AreEqual("b",strArr[1]);
|
|
|
|
|
Assert.AreEqual("c c",strArr[2]);
|
2022-03-22 16:53:24 +08:00
|
|
|
|
str = null;
|
|
|
|
|
strArr = str.SplitStr();
|
|
|
|
|
Assert.IsNotNull(strArr);
|
2022-12-09 11:17:04 +08:00
|
|
|
|
Assert.AreEqual(0,strArr.Length);
|
2022-05-24 15:53:26 +08:00
|
|
|
|
|
|
|
|
|
//指定分隔符
|
|
|
|
|
str = $"a,b.c;d,e。f;g";
|
|
|
|
|
|
|
|
|
|
strArr = str.SplitStr(',');
|
2022-12-09 11:17:04 +08:00
|
|
|
|
Assert.AreEqual(2,strArr.Length,"分割后长度应该为7");
|
|
|
|
|
Assert.AreEqual("a",strArr[0]);
|
|
|
|
|
Assert.AreEqual("b.c;d,e。f;g",strArr[1]);
|
|
|
|
|
|
|
|
|
|
strArr = str.SplitStr(',','.');
|
|
|
|
|
Assert.AreEqual(3,strArr.Length,"分割后长度应该为7");
|
|
|
|
|
Assert.AreEqual("a",strArr[0]);
|
|
|
|
|
Assert.AreEqual("b",strArr[1]);
|
|
|
|
|
Assert.AreEqual("c;d,e。f;g",strArr[2]);
|
2022-05-24 15:53:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试ToDefault
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestToDefault() {
|
|
|
|
|
var str = "";
|
|
|
|
|
var r = "";
|
|
|
|
|
r = str.ToDefault("abc");
|
2022-12-09 11:17:04 +08:00
|
|
|
|
Assert.AreEqual("abc",r);
|
2022-05-24 15:53:26 +08:00
|
|
|
|
str = null;
|
|
|
|
|
r = str.ToDefault("abc");
|
2022-12-09 11:17:04 +08:00
|
|
|
|
Assert.AreEqual("abc",r);
|
2022-05-24 15:53:26 +08:00
|
|
|
|
str = "123";
|
|
|
|
|
r = str.ToDefault("abc");
|
2022-12-09 11:17:04 +08:00
|
|
|
|
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");
|
2022-03-22 16:53:24 +08:00
|
|
|
|
}
|
2024-10-22 10:14:23 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 去除头尾字符
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestTrimString() {
|
|
|
|
|
var str = @"abcabcffe222256256";
|
|
|
|
|
//去除头部的abc字符
|
|
|
|
|
var r = str.TrimString("abc");
|
|
|
|
|
Assert.AreEqual(r , "ffe222256256");
|
|
|
|
|
//去除尾部256字符
|
|
|
|
|
r = str.TrimString("","256");
|
|
|
|
|
Assert.AreEqual(r,"abcabcffe222");
|
|
|
|
|
//去除头部abc字符和尾部256字符
|
|
|
|
|
r = str.TrimString("abc","256");
|
|
|
|
|
Assert.AreEqual(r,"ffe222");
|
|
|
|
|
}
|
2022-03-22 16:53:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|