增加string.TrimString扩展方法,可以去除头尾给定字符串
This commit is contained in:
parent
eb45aa189a
commit
e4a54ddd34
|
@ -112,5 +112,22 @@ namespace Falcon.SugarApi.Test
|
||||||
Assert.IsTrue(result.Length == 1);
|
Assert.IsTrue(result.Length == 1);
|
||||||
Assert.IsTrue(result[0] == "abc");
|
Assert.IsTrue(result[0] == "abc");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ namespace Falcon.SugarApi
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="str">字符串</param>
|
/// <param name="str">字符串</param>
|
||||||
/// <returns>空或null为True,否则False</returns>
|
/// <returns>空或null为True,否则False</returns>
|
||||||
public static bool IsNullOrEmpty(this string? str) => str==null||string.IsNullOrEmpty(str);
|
public static bool IsNullOrEmpty(this string? str) => str == null || string.IsNullOrEmpty(str);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 字符串是否不为空或null
|
/// 字符串是否不为空或null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -38,7 +38,7 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="splitChars"></param>
|
/// <param name="splitChars"></param>
|
||||||
/// <returns>字符串数组。当str为null时返回空数组</returns>
|
/// <returns>字符串数组。当str为null时返回空数组</returns>
|
||||||
public static string[] SplitStr(this string? str,params char[] splitChars)
|
public static string[] SplitStr(this string? str,params char[] splitChars)
|
||||||
=> str.IsNullOrEmpty() ? Array.Empty<string>() : str.Split(splitChars.Length==0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries|StringSplitOptions.TrimEntries);
|
=> str.IsNullOrEmpty() ? Array.Empty<string>() : str.Split(splitChars.Length == 0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将字符串按格式转换为时间格式
|
/// 将字符串按格式转换为时间格式
|
||||||
|
@ -48,7 +48,7 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
|
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
|
||||||
/// <returns>时间</returns>
|
/// <returns>时间</returns>
|
||||||
public static DateTime ToDateTime(this string str,string dateTimeFormat = "",CultureInfo? culture = null) {
|
public static DateTime ToDateTime(this string str,string dateTimeFormat = "",CultureInfo? culture = null) {
|
||||||
culture??=CultureInfo.InvariantCulture;
|
culture ??= CultureInfo.InvariantCulture;
|
||||||
if(dateTimeFormat.IsNullOrEmpty()) {
|
if(dateTimeFormat.IsNullOrEmpty()) {
|
||||||
return DateTime.Parse(str);
|
return DateTime.Parse(str);
|
||||||
}
|
}
|
||||||
|
@ -64,11 +64,11 @@ namespace Falcon.SugarApi
|
||||||
/// <returns>成功True,失败False</returns>
|
/// <returns>成功True,失败False</returns>
|
||||||
public static bool TryToDateTime(this string str,string dateTimeFormat,out DateTime dt,CultureInfo? culture = null) {
|
public static bool TryToDateTime(this string str,string dateTimeFormat,out DateTime dt,CultureInfo? culture = null) {
|
||||||
try {
|
try {
|
||||||
dt=str.ToDateTime(dateTimeFormat,culture);
|
dt = str.ToDateTime(dateTimeFormat,culture);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch(Exception) {
|
catch(Exception) {
|
||||||
dt=default;
|
dt = default;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,18 +112,18 @@ namespace Falcon.SugarApi
|
||||||
/// <returns>子字符串</returns>
|
/// <returns>子字符串</returns>
|
||||||
/// <exception cref="Exception">获取长度不满足要求</exception>
|
/// <exception cref="Exception">获取长度不满足要求</exception>
|
||||||
public static string SubstringEx(this string str,int start,int length) {
|
public static string SubstringEx(this string str,int start,int length) {
|
||||||
if(start>str.Length||length==0) {
|
if(start > str.Length || length == 0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if(length<0) {
|
if(length < 0) {
|
||||||
start=start+length;
|
start = start + length;
|
||||||
length=-length;
|
length = -length;
|
||||||
}
|
}
|
||||||
if(start<0) {
|
if(start < 0) {
|
||||||
length+=start;
|
length += start;
|
||||||
start=0;
|
start = 0;
|
||||||
}
|
}
|
||||||
if(start+length>str.Length) {
|
if(start + length > str.Length) {
|
||||||
return str.Substring(start);
|
return str.Substring(start);
|
||||||
}
|
}
|
||||||
return str.Substring(start,length);
|
return str.Substring(start,length);
|
||||||
|
@ -136,17 +136,50 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="length">分割长度</param>
|
/// <param name="length">分割长度</param>
|
||||||
/// <returns>分割后的字符串枚举</returns>
|
/// <returns>分割后的字符串枚举</returns>
|
||||||
public static IEnumerable<string> Split(this string str,int length) {
|
public static IEnumerable<string> Split(this string str,int length) {
|
||||||
if(length<=0) {
|
if(length <= 0) {
|
||||||
throw new Exception("分割长度必须大于0");
|
throw new Exception("分割长度必须大于0");
|
||||||
}
|
}
|
||||||
if(str.IsNullOrEmpty()) {
|
if(str.IsNullOrEmpty()) {
|
||||||
throw new ArgumentNullException(nameof(str));
|
throw new ArgumentNullException(nameof(str));
|
||||||
}
|
}
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(str.Length>i) {
|
while(str.Length > i) {
|
||||||
yield return str.SubstringEx(i,length);
|
yield return str.SubstringEx(i,length);
|
||||||
i+=length;
|
i += length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从字符串开头去除startStr字符串并从末尾去除endStr字符串,如果有多个会全部去除。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str">原始字符串</param>
|
||||||
|
/// <param name="startStr">开始处要去除的字符串,不需要则传入空字符串</param>
|
||||||
|
/// <param name="endStr">末尾处要去除的字符串,不需要则传入空字符串</param>
|
||||||
|
/// <returns>结果字符串</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user