From e4a54ddd342a139b0855055e2bf1a667243e5662 Mon Sep 17 00:00:00 2001
From: Falcon <12919280+falconfly@user.noreply.gitee.com>
Date: Tue, 22 Oct 2024 10:14:23 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0string.TrimString=E6=89=A9?=
=?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=8E=BB?=
=?UTF-8?q?=E9=99=A4=E5=A4=B4=E5=B0=BE=E7=BB=99=E5=AE=9A=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi.Test/StringExtendTest.cs | 17 +++++++
Falcon.SugarApi/StringExtend.cs | 65 ++++++++++++++++++------
2 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/Falcon.SugarApi.Test/StringExtendTest.cs b/Falcon.SugarApi.Test/StringExtendTest.cs
index 84f68ab..c2b1258 100644
--- a/Falcon.SugarApi.Test/StringExtendTest.cs
+++ b/Falcon.SugarApi.Test/StringExtendTest.cs
@@ -112,5 +112,22 @@ namespace Falcon.SugarApi.Test
Assert.IsTrue(result.Length == 1);
Assert.IsTrue(result[0] == "abc");
}
+
+ ///
+ /// 去除头尾字符
+ ///
+ [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");
+ }
}
}
diff --git a/Falcon.SugarApi/StringExtend.cs b/Falcon.SugarApi/StringExtend.cs
index 6fed262..ac97702 100644
--- a/Falcon.SugarApi/StringExtend.cs
+++ b/Falcon.SugarApi/StringExtend.cs
@@ -15,7 +15,7 @@ namespace Falcon.SugarApi
///
/// 字符串
/// 空或null为True,否则False
- public static bool IsNullOrEmpty(this string? str) => str==null||string.IsNullOrEmpty(str);
+ public static bool IsNullOrEmpty(this string? str) => str == null || string.IsNullOrEmpty(str);
///
/// 字符串是否不为空或null
///
@@ -38,7 +38,7 @@ namespace Falcon.SugarApi
///
/// 字符串数组。当str为null时返回空数组
public static string[] SplitStr(this string? str,params char[] splitChars)
- => str.IsNullOrEmpty() ? Array.Empty() : str.Split(splitChars.Length==0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries|StringSplitOptions.TrimEntries);
+ => str.IsNullOrEmpty() ? Array.Empty() : str.Split(splitChars.Length == 0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
///
/// 将字符串按格式转换为时间格式
@@ -48,7 +48,7 @@ namespace Falcon.SugarApi
/// 区域特性.默认CultureInfo.InvariantCulture
/// 时间
public static DateTime ToDateTime(this string str,string dateTimeFormat = "",CultureInfo? culture = null) {
- culture??=CultureInfo.InvariantCulture;
+ culture ??= CultureInfo.InvariantCulture;
if(dateTimeFormat.IsNullOrEmpty()) {
return DateTime.Parse(str);
}
@@ -64,11 +64,11 @@ namespace Falcon.SugarApi
/// 成功True,失败False
public static bool TryToDateTime(this string str,string dateTimeFormat,out DateTime dt,CultureInfo? culture = null) {
try {
- dt=str.ToDateTime(dateTimeFormat,culture);
+ dt = str.ToDateTime(dateTimeFormat,culture);
return true;
}
catch(Exception) {
- dt=default;
+ dt = default;
return false;
}
}
@@ -112,18 +112,18 @@ namespace Falcon.SugarApi
/// 子字符串
/// 获取长度不满足要求
public static string SubstringEx(this string str,int start,int length) {
- if(start>str.Length||length==0) {
+ if(start > str.Length || length == 0) {
return "";
}
- if(length<0) {
- start=start+length;
- length=-length;
+ if(length < 0) {
+ start = start + length;
+ length = -length;
}
- if(start<0) {
- length+=start;
- start=0;
+ if(start < 0) {
+ length += start;
+ start = 0;
}
- if(start+length>str.Length) {
+ if(start + length > str.Length) {
return str.Substring(start);
}
return str.Substring(start,length);
@@ -136,17 +136,50 @@ namespace Falcon.SugarApi
/// 分割长度
/// 分割后的字符串枚举
public static IEnumerable Split(this string str,int length) {
- if(length<=0) {
+ if(length <= 0) {
throw new Exception("分割长度必须大于0");
}
if(str.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(str));
}
int i = 0;
- while(str.Length>i) {
+ while(str.Length > i) {
yield return str.SubstringEx(i,length);
- i+=length;
+ i += length;
}
}
+
+ ///
+ /// 从字符串开头去除startStr字符串并从末尾去除endStr字符串,如果有多个会全部去除。
+ ///
+ /// 原始字符串
+ /// 开始处要去除的字符串,不需要则传入空字符串
+ /// 末尾处要去除的字符串,不需要则传入空字符串
+ /// 结果字符串
+ public static string TrimString(this string str,string startStr = "",string endStr = "") {
+ if(str == "") {
+ return str;
+ }
+ if(startStr != "") {
+ while(true) {
+ var p = str.IndexOf(startStr);
+ if(p < 0) {
+ break;
+ }
+ str = str.Substring(p + startStr.Length);
+ }
+ }
+ if(endStr != "") {
+ while(true) {
+ var p = str.LastIndexOf(endStr);
+ if(p < 0) {
+ break;
+ }
+ str = str.Substring(0,p);
+ }
+ }
+ return str;
+ }
+
}
}
\ No newline at end of file