Skip to content

Class StringExtension

Namespace: Tool
Assembly: Tool.Net.dll

对string类进行升级

csharp
public static class StringExtension

Inheritance

objectStringExtension

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

ObjectExtension.Add<T>(object, object, object), ObjectExtension.CopyEntity(object, object, params string[]), ObjectExtension.EntityToJson(object), ObjectExtension.EntityToJson(object, bool), ObjectExtension.EntityToJson(object, bool, string), DictionaryExtension.GetDictionary(object), TypeExtension.GetFieldKey(object, string, out bool), TypeExtension.GetFieldKey<T>(object, string, out bool), TypeExtension.GetFieldKey(object, Type, string, out bool), ObjectExtension.GetIntPtr(object), ObjectExtension.GetIntPtrInt(object), TypeExtension.GetPropertieFind(object, string, bool), TypeExtension.GetProperties(object), TypeExtension.GetPropertyKey(object, string, out bool), TypeExtension.GetPropertyKey<T>(object, string, out bool), TypeExtension.GetPropertyKey(object, Type, string, out bool), TypeExtension.GetValue(object, PropertyDescriptor), TypeExtension.GetValue(object, string, bool), TypeExtension.GetValue(object, string), ObjectExtension.Read<T>(object, object, int, int), ObjectExtension.Read<T>(object, int, object, int, int), DictionaryExtension.SetDictionary(object, IDictionary<string, object>), TypeExtension.SetFieldKey(object, string, object), TypeExtension.SetFieldKey<T>(object, string, object), TypeExtension.SetFieldKey(object, Type, string, object), TypeExtension.SetPropertyKey(object, string, object), TypeExtension.SetPropertyKey<T>(object, string, object), TypeExtension.SetPropertyKey(object, Type, string, object), TypeExtension.SetValue(object, PropertyDescriptor, object), TypeExtension.SetValue(object, string, object, bool), TypeExtension.SetValue(object, string, object), ObjectExtension.ToBase64String(object), ObjectExtension.ToBytes(object), ObjectExtension.ToBytes(object, out Type), DictionaryExtension.ToDictionary(object), DictionaryExtension.ToDictionary<T>(object), DictionaryExtension.ToIDictionary(object), DictionaryExtension.ToIDictionary<T>(object), ObjectExtension.ToJson(object), ObjectExtension.ToJson(object, JsonSerializerOptions), ObjectExtension.ToJsonWeb(object), ObjectExtension.ToJsonWeb(object, Action<JsonSerializerOptions>), ObjectExtension.ToTryVar<T>(object, T), ObjectExtension.ToVar<T>(object), ObjectExtension.ToVar(object, Type, bool), ObjectExtension.ToVar(object, string), ObjectExtension.ToXml(object)

Remarks

代码由逆血提供支持

Fields

sqlStr

默认包含的(防止注入类型)

csharp
public const string sqlStr = "dbo.|.dbo.|break;|case |when |between |then |chr |add |alter |create |net |cmd=|while |count |union |from |use |and |top |or |iframe |script |insert |delete |select |update |exec |char |varchar |mid |drop |declare |commit |rollback | tran|truncate | where | in |cursor |exec |begin |open |xp_|sp_|master |--|0x"

Field Value

string

Methods

Add(string[], string[], string)

给数组加新的值

csharp
public static void Add(this string[] txts, string[] _txts, string txt)

Parameters

txts string[]

string[]

_txts string[]

新数组(为空,但是必须大于原数组一个下标以上)

txt string

新增的值

Change(string[], string, int)

修改当前数组的某个下标的内容

csharp
public static void Change(this string[] txts, string ChangeTxt, int length)

Parameters

txts string[]

源数据

ChangeTxt string

修改的内容

length int

下标位置

Contains(string[], string)

查找该string数组中是否存在该值。

csharp
public static bool Contains(this string[] txt, string txt1)

Parameters

txt string[]

string[]

txt1 string

查找的字符

Returns

bool

方法存在或不存在

ConvertHzToPz_Gb2312(string)

将字符串中的中文转成拼音

csharp
public static string ConvertHzToPz_Gb2312(this string value)

Parameters

value string

字符串

Returns

string

返回完整的拼音

DirtyContainsAny(string, params string[])

对当前字符串进行脏字验证

csharp
public static bool DirtyContainsAny(this string text, params string[] texts)

Parameters

text string

String

texts string[]

自定义脏字数组

Returns

bool

存在返回,true否则为false

DirtyContainsAny(string, string)

对当前字符串进行脏字验证

csharp
public static bool DirtyContainsAny(this string text, string dirty)

Parameters

text string

String

dirty string

自定义脏字字符串以“|”分割

Returns

bool

存在返回,true否则为false

DirtyContainsAnys(string, params string[])

对当前字符串进行脏字验证,返回存在的所有脏字,及下标

csharp
public static object DirtyContainsAnys(this string text, params string[] texts)

Parameters

text string

String

texts string[]

自定义脏字数组

Returns

object

返回存在的所有脏字,及下标

DirtyContainsAnys(string, string)

对当前字符串进行脏字验证,返回存在的所有脏字,及下标

csharp
public static object DirtyContainsAnys(this string text, string dirty)

Parameters

text string

String

dirty string

自定义脏字字符串以“|”分割

Returns

object

返回存在的所有脏字,及下标

DirtyDetection(string, string[], char)

对当前字符串进行脏字检测,并净化

csharp
public static string DirtyDetection(this string text, string[] texts, char symbol)

Parameters

text string

String

texts string[]

自定义脏字数组

symbol char

将存在的脏字替换为???

Returns

string

返回被净化后字符串

DirtyDetection(string, string[])

对当前字符串进行脏字检测,并净化

csharp
public static string DirtyDetection(this string text, string[] texts)

Parameters

text string

String

texts string[]

自定义脏字数组

Returns

string

返回被净化后字符串

DirtyDetection(string, string)

对当前字符串进行脏字检测,并净化

csharp
public static string DirtyDetection(this string text, string dirty)

Parameters

text string

String

dirty string

自定义脏字字符串以“|”分割

Returns

string

返回被净化后字符串

DirtyDetection(string, string, char)

对当前字符串进行脏字检测,并净化

csharp
public static string DirtyDetection(this string text, string dirty, char symbol)

Parameters

text string

String

dirty string

自定义脏字字符串以“|”分割

symbol char

将存在的脏字替换为???

Returns

string

返回被净化后字符串

EqualsNotCase(string, string)

对吧字符串是否相同,注明:不区分大小写的。

csharp
public static bool EqualsNotCase(this string txt, string txt1)

Parameters

txt string

字符串

txt1 string

对比字符串

Returns

bool

返回bool类型

GetArrayIndex(string[], int, int)

同于获取指定部分的内容

csharp
public static string[] GetArrayIndex(this string[] obj, int index, int count)

Parameters

obj string[]

对象数组

index int

从下标N开始

count int

到下标N结束

Returns

string[]

返回一部分的数组内容

GetGuid(bool)

由连字符分隔的32位随机数

csharp
public static string GetGuid(bool type = true)

Parameters

type bool

返回两个结果,true是去掉分隔符的,false是未去掉的

Returns

string

GuidTo16String()

根据GUID获取16位的唯一字符串

csharp
public static string GuidTo16String()

Returns

string

GuidToLongID()

根据GUID获取19位的唯一数字序列

csharp
public static long GuidToLongID()

Returns

long

Json(string)

转换成对象 (默认允许Json中包含注释)

csharp
public static Dictionary<string, object> Json(this string txt)

Parameters

txt string

String

Returns

Dictionary<string, object>

转换成Dictionary对象

Json(string, JsonDocumentOptions)

转换成对象

csharp
public static Dictionary<string, object> Json(this string txt, JsonDocumentOptions options = default)

Parameters

txt string

String

options JsonDocumentOptions

解析时用到的配置信息

Returns

Dictionary<string, object>

转换成Dictionary对象

Json<T>(string)

转换成实体类

csharp
public static T Json<T>(this string txt)

Parameters

txt string

String

Returns

T

转换成实体类

Type Parameters

T

转换的实体

Json<T>(string, JsonSerializerOptions)

转换成实体类

csharp
public static T Json<T>(this string txt, JsonSerializerOptions jsonSerializerOptions)

Parameters

txt string

String

jsonSerializerOptions JsonSerializerOptions

需要的序列化条件

Returns

T

转换成实体类

Type Parameters

T

转换的实体

JsonDynamic(string, JsonDocumentOptions)

转换成虚构实体对象

csharp
public static dynamic JsonDynamic(this string txt, JsonDocumentOptions options = default)

Parameters

txt string

String

options JsonDocumentOptions

解析时用到的配置信息

Returns

dynamic

转换成虚构实体对象

JsonList(string)

转换成 List{Dictionary{string, object}} 对象 (默认允许Json中包含注释)

csharp
public static List<Dictionary<string, object>> JsonList(this string txt)

Parameters

txt string

String

Returns

List<Dictionary<string, object>>

转换成Dictionary对象

JsonList(string, JsonDocumentOptions)

转换成 List{Dictionary{string, object}} 对象

csharp
public static List<Dictionary<string, object>> JsonList(this string txt, JsonDocumentOptions options = default)

Parameters

txt string

String

options JsonDocumentOptions

解析时用到的配置信息

Returns

List<Dictionary<string, object>>

转换成Dictionary对象

JsonList<T>(string)

转换成实体数组

csharp
public static List<T> JsonList<T>(this string txt)

Parameters

txt string

String

Returns

List<T>

转换成实体数组

Type Parameters

T

转换的实体

JsonList<T>(string, JsonSerializerOptions)

转换成实体数组

csharp
public static List<T> JsonList<T>(this string txt, JsonSerializerOptions jsonSerializerOptions)

Parameters

txt string

String

jsonSerializerOptions JsonSerializerOptions

需要的序列化条件

Returns

List<T>

转换成实体数组

Type Parameters

T

转换的实体

JsonObject(string, JsonDocumentOptions)

转换成对象

csharp
public static object JsonObject(this string txt, JsonDocumentOptions options = default)

Parameters

txt string

String

options JsonDocumentOptions

解析时用到的配置信息

Returns

object

对象

Exceptions

SystemException

该字符串不存在任何内容!

JsonVar(string, JsonDocumentOptions)

一种获取 Json 格式数据的实现

csharp
public static JsonVar JsonVar(this string txt, JsonDocumentOptions options = default)

Parameters

txt string

Json 格式字符串

options JsonDocumentOptions

解析时用到的配置信息

Returns

JsonVar

转换成特殊结构对象,用于获取值

MD5Lower(string, bool)

获取加密的MD5,小写的(特意备注:这里是指将你输入的字符串加密成MD5后转为小写,有些傻子以为是先将字符串转小写后加密)

csharp
public static string MD5Lower(this string md5_txt, bool IsMD5 = true)

Parameters

md5_txt string

String

IsMD5 bool

表示加密为:32位或者16位(默认32位)

Returns

string

返回MD5

MD5Upper(string, bool)

获取加密的MD5,大写的(特意备注:这里是指将你输入的字符串加密成MD5后转为大写,有些傻子以为是先将字符串转大写后加密)

csharp
public static string MD5Upper(this string md5_txt, bool IsMD5 = true)

Parameters

md5_txt string

String

IsMD5 bool

表示加密为:32位或者16位(默认32位)

Returns

string

返回MD5

SHA1(string)

将字符串进行SHA1加密

csharp
public static string SHA1(this string sha_txt)

Parameters

sha_txt string

String

Returns

string

返回SHA1

SHA256(string)

将字符串进行SHA256加密

csharp
public static string SHA256(this string sha256_txt)

Parameters

sha256_txt string

String

Returns

string

返回SHA256

SQLFilter(string)

过滤非法关键字(使用自带常量)

csharp
public static bool SQLFilter(this string keyword)

Parameters

keyword string

带验证的SQL

Returns

bool

SQLFilter(string, string)

过滤非法关键字,这个可以按照项目灵活配置

csharp
public static bool SQLFilter(this string keyword, string sqlStr)

Parameters

keyword string

带验证的SQL

sqlStr string

验证的防注入类型字符串,以“|”区分

Returns

bool

SQLFilter(string, params string[])

过滤非法关键字,这个可以按照项目灵活配置

csharp
public static bool SQLFilter(this string keyword, params string[] sqlStr)

Parameters

keyword string

带验证的SQL

sqlStr string[]

验证的防注入类型(必须大写)

Returns

bool

Split(string, params string[])

返回的字符串数组包含此字符串中的子字符串(由指定字符串数组的元素分隔)。 参数指定是否返回空数组元素。

csharp
public static string[] Split(this string txt, params string[] separator)

Parameters

txt string

字符串

separator string[]

指定字符串数组的元素分隔

Returns

string[]

返回一个数组

StrToPinyin(string)

获得一个字符串的汉语拼音码 (首字母)

csharp
public static string StrToPinyin(this string value)

Parameters

value string

字符串

Returns

string

汉语拼音码,该字符串只包含大写的英文字母

StringDecode(string)

使用指定的编码对象对 string 字符串进行解码。

csharp
public static string StringDecode(this string txt)

Parameters

txt string

string

Returns

string

返回解码结果

StringDecode(string, Encoding)

使用指定的编码对象对 string 字符串进行解码。

csharp
public static string StringDecode(this string txt, Encoding encoding)

Parameters

txt string

encoding Encoding

指定编码格式

Returns

string

返回解码结果

StringEncode(string)

使用指定的编码对象对 string 字符串进行编码。

csharp
public static string StringEncode(this string txt)

Parameters

txt string

string

Returns

string

返回编码结果

StringEncode(string, Encoding)

使用指定的编码对象对 string 字符串进行编码。

csharp
public static string StringEncode(this string txt, Encoding encoding)

Parameters

txt string

string

encoding Encoding

指定编码格式

Returns

string

返回编码结果

ToBytes(string)

返回二进制流

csharp
public static byte[] ToBytes(this string txt)

Parameters

txt string

String

Returns

byte[]

返回二进制流

ToBytes(string, Encoding)

返回二进制流

csharp
public static byte[] ToBytes(this string txt, Encoding encoding)

Parameters

txt string

String

encoding Encoding

指定格式

Returns

byte[]

返回二进制流

ToDateTime(string)

返回时间类型

csharp
public static DateTime ToDateTime(this string txt)

Parameters

txt string

String

Returns

DateTime

返回时间类型

ToDecimal(string)

返回十进制数

csharp
public static decimal ToDecimal(this string txt)

Parameters

txt string

String

Returns

decimal

返回十进制数

ToDouble(string)

返回双精度浮点数

csharp
public static double ToDouble(this string txt)

Parameters

txt string

String

Returns

double

返回双精度浮点数

ToFileBytes(string)

将文件转换为byte数组(如果没有找到则返回零的数组)

csharp
public static byte[] ToFileBytes(this string path)

Parameters

path string

文件地址(绝对路径)

Returns

byte[]

转换后的byte数组(如果没有找到则返回零的数组)

ToInt(string)

返回Int类型

csharp
public static int ToInt(this string txt)

Parameters

txt string

String

Returns

int

返回Int类型

ToInts(string[])

将String数组转成Int数组

csharp
public static int[] ToInts(this string[] txt)

Parameters

txt string[]

String[]

Returns

int[]

注释:如出现无法转换的,这抛出异常

ToLong(string)

返回Int类型

csharp
public static long ToLong(this string txt)

Parameters

txt string

String

Returns

long

返回Int类型

UnBase64String(string)

将对Base64字符串换成象转(解码)

csharp
public static byte[] UnBase64String(this string value)

Parameters

value string

Base64字符串

Returns

byte[]

返回一个源对象

Xml<T>(string)

将Xml格式字符串转换为对象

csharp
public static T Xml<T>(this string txt) where T : class

Parameters

txt string

要转类型XML字符串

Returns

T

对象

Type Parameters

T

基于Apache-2.0协议开源