Files
HTCloud/HT.Cloud.Code/Extend/Ext.String.cs
2023-03-03 16:07:50 +08:00

37 lines
1.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
namespace HT.Cloud.Code
{
/// <summary>
/// string扩展类
/// </summary>
public static partial class Extensions
{
/// <summary>
/// 从分隔符开始向尾部截取字符串
/// </summary>
/// <param name="this">源字符串</param>
/// <param name="separator">分隔符</param>
/// <param name="lastIndexOf">true从最后一个匹配的分隔符开始截取false从第一个匹配的分隔符开始截取默认true</param>
/// <returns>string</returns>
public static string Substring(this string @this, string separator, bool lastIndexOf = true)
{
var startIndex = (lastIndexOf ?
@this.LastIndexOf(separator, StringComparison.OrdinalIgnoreCase) :
@this.IndexOf(separator, StringComparison.OrdinalIgnoreCase)) +
separator.Length;
var length = @this.Length - startIndex;
return @this.Substring(startIndex, length);
}
#region
public static string ReplaceFrist(this string str, string oldChar, string newChar)
{
int idx = str.IndexOf(oldChar);
str = str.Remove(idx, oldChar.Length);
str = str.Insert(idx, newChar);
return str;
}
#endregion
}
}