Class StringExtensions
string 扩展方法
Inherited Members
Namespace: Lett.Extensions
Assembly: Lett.Extensions.dll
Syntax
public static class StringExtensions
Methods
| Improve this Doc View SourceAppendPrefixIfNotExist(String, String, StringComparison)
如果字符串不包含指定前缀,则添加前缀
默认不区分大小写
Declaration
public static string AppendPrefixIfNotExist(this string this, string prefix, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | prefix | 前缀 |
StringComparison | stringComparison | 字符串比较规则 默认使用 OrdinalIgnoreCase |
Returns
Type | Description |
---|---|
String |
Examples
var rs1 = "aa".AppendPrefixIfNotExist("t_"); // t_aa
var rs2 = "T_bb".AppendPrefixIfNotExist("t_"); // T_bb
var rs3 = "T_cc".AppendPrefixIfNotExist("t_", StringComparison.Ordinal); // t_T_cc
"T_cc".AppendPrefixIfNotExist(null); // throw ArgumentNullException prefix is null
default(string).AppendPrefixIfNotExist("d"); // throw ArgumentNullException @this is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
AppendSuffixIfNotExist(String, String, StringComparison)
如果字符串不包含指定后缀,则添加后缀
默认不区分大小写
Declaration
public static string AppendSuffixIfNotExist(this string this, string suffix, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | suffix | 后缀 |
StringComparison | stringComparison | 字符串比较规则 默认使用 OrdinalIgnoreCase |
Returns
Type | Description |
---|---|
String |
Examples
var rs1 = "aa".AppendSuffixIfNotExist("_t"); // aa_t
var rs2 = "bb_T".AppendSuffixIfNotExist("_t"); // bb_T
var rs3 = "cc_T".AppendSuffixIfNotExist("_t", StringComparison.Ordinal); // cc_T_t
"cc_T".AppendSuffixIfNotExist(null); // throw ArgumentNullException suffix is null
default(string).AppendSuffixIfNotExist("_t"); // throw ArgumentNullException @this is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
AsDirectoryInfo(String)
转换为 DirectoryInfo (当前字符串作为 path)
Declaration
public static DirectoryInfo AsDirectoryInfo(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
DirectoryInfo |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
AsFileStream(String, FileMode, FileAccess, FileShare, Int32)
转换为 FileStream (当前字符串作为 path)
Declaration
public static FileStream AsFileStream(this string this, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, int bufferSize = 8192)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
FileMode | fileMode | 文件打开方式 |
FileAccess | fileAccess | |
FileShare | fileShare | |
Int32 | bufferSize | 缓冲区大小 |
Returns
Type | Description |
---|---|
FileStream |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
|
NotSupportedException |
|
FileNotFoundException | The file cannot be found, such as when |
IOException | An I/O error, such as specifying FileMode.CreateNew when the file specified by |
SecurityException | The caller does not have the required permission. |
DirectoryNotFoundException | The specified path is invalid, such as being on an unmapped drive. |
UnauthorizedAccessException | The |
PathTooLongException | The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. |
Base64Decode(String)
进行 BASE6 4解码
使用 UTF8
Declaration
public static string Base64Decode(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
String | 转换失败返回 |
Examples
var base64 = "QUJDRA==";
base64.Base64Decode(); // "ABCD"
|
Improve this Doc
View Source
Base64Encode(String)
进行 BASE64 编码
使用 UTF8
Declaration
public static string Base64Encode(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
String | 转换失败返回 |
Examples
var str1 = "ABCD";
str1.Base64Encode(); // "QUJDRA=="
|
Improve this Doc
View Source
ContainsAll(String, IEnumerable<String>)
是否全部包含
使用 CurrentCultureIgnoreCase 比较
Declaration
public static bool ContainsAll(this string this, IEnumerable<string> values)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
IEnumerable<String> | values | 需要进行判断的字符串集合 |
Returns
Type | Description |
---|---|
Boolean |
Examples
var a = "aaabbbccc";
var b = new[] {"aaa", "bbb"};
a.ContainsAll(b); // true
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
ContainsAll(String, IEnumerable<String>, StringComparison)
是否全部包含
Declaration
public static bool ContainsAll(this string this, IEnumerable<string> values, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
IEnumerable<String> | values | 需要进行判断的字符串集合 |
StringComparison | comparisonType | 字符串比较规则 |
Returns
Type | Description |
---|---|
Boolean |
Examples
var a = "aaabbbccc";
var b = new[] {"aaa", "bbb"};
a.ContainsAll(b); // true
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
ContainsAny(String, IEnumerable<String>)
是否包含任意一个
使用 CurrentCultureIgnoreCase 判断
Declaration
public static bool ContainsAny(this string this, IEnumerable<string> values)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
IEnumerable<String> | values | 需要进行判断的字符串集合 |
Returns
Type | Description |
---|---|
Boolean |
Examples
var a = "aaabbbccc";
var b = new[] {"a", "dd", "eee"};
a.ContainsAny(b); // true
|
Improve this Doc
View Source
ContainsAny(String, IEnumerable<String>, StringComparison)
是否包含任意一个
Declaration
public static bool ContainsAny(this string this, IEnumerable<string> values, StringComparison comparisonType)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
IEnumerable<String> | values | 需要进行判断的字符串集合 |
StringComparison | comparisonType | 字符串比较规则 |
Returns
Type | Description |
---|---|
Boolean |
Examples
var a = "aaabbbccc";
var b = new[] {"a", "dd", "eee"};
a.ContainsAny(b); // true
|
Improve this Doc
View Source
Format(String, Object[])
格式化
Declaration
public static string Format(this string this, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
Object[] | args |
Returns
Type | Description |
---|---|
String |
Examples
var tmp = "{0}-{1}";
tmp.Format(new[]{"aaa","bbb"}); // "aaa-bbb"
Exceptions
Type | Condition |
---|---|
ArgumentNullException | |
FormatException |
IgnoreCaseEquals(String, String)
忽略大小写比较
Declaration
public static bool IgnoreCaseEquals(this string this, string value)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | value | 需要进行比较的字符串 |
Returns
Type | Description |
---|---|
Boolean |
Examples
"aaa".IgnoreCaseEquals("AaA"); // true
|
Improve this Doc
View Source
IsEmail(String)
是否Email
Declaration
public static bool IsEmail(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
"abc@qqq.com".IsEmail(); // true
"abc@qqq#.com".IsEmail(); // false
Exceptions
Type | Condition |
---|---|
ArgumentNullException | |
ArgumentNullException | |
ArgumentException | |
RegexMatchTimeoutException |
IsEmpty(String)
是否空字符串
Declaration
public static bool IsEmpty(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
var a = "";
var b = " ";
a.IsEmpty(); // true
b.IsEmpty(); // false
|
Improve this Doc
View Source
IsLike(String, String)
Declaration
public static bool IsLike(this string this, string pattern)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 通配符表达式,为 null 时返回 false |
Returns
Type | Description |
---|---|
Boolean |
|
Examples
var test1 = "abcdefg\r\nabcdefghijk";
test1.IsLike("abc*"); // true
test1.IsLike("a*"); // true
test1.IsLike("*ijk"); // true
test1.IsLike("abc*fg*"); // true
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | |
ArgumentNullException | |
FormatException | |
ArgumentException | |
RegexMatchTimeoutException |
IsLower(String)
是否全部小写
Declaration
public static bool IsLower(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
"abcddd".IsLower(); // true;
"abDD".IsLower(); // false
|
Improve this Doc
View Source
IsNullOrEmpty(String)
是否 null 或 string.Empty
Declaration
public static bool IsNullOrEmpty(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
" ".IsNullOrEmpty(); // false
"".IsNullOrEmpty(); // true
|
Improve this Doc
View Source
IsNullOrWhiteSpace(String)
是否null或空白
Declaration
public static bool IsNullOrWhiteSpace(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
" ".IsNullOrWhiteSpace(); // true
"\r".IsNullOrWhiteSpace(); // true
|
Improve this Doc
View Source
IsUpper(String)
是否全部大写
Declaration
public static bool IsUpper(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
"ABC".IsUpper(); // true
"ABd".IsUpper(); // false
|
Improve this Doc
View Source
IsUrl(String)
是否URL
Declaration
public static bool IsUrl(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
"http://sdf.com".IsUrl(); // true
"http://sdf.c.dddd.cccc.saaa.com".IsUrl(); // true
"www.sdf.com".IsUrl(); // false
|
Improve this Doc
View Source
IsWhiteSpace(String)
是否空白字符串
Declaration
public static bool IsWhiteSpace(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Boolean |
Examples
var a = "";
var b = " ";
var c = "\r";
var d = "\t";
var e = "\n";
var f = "\r \t";
a.IsWhiteSpace(); // true
b.IsWhiteSpace(); // true
c.IsWhiteSpace(); // true
d.IsWhiteSpace(); // true
e.IsWhiteSpace(); // true
f.IsWhiteSpace(); // true
|
Improve this Doc
View Source
Left(String, Int32)
从左侧返回指定长度的字符串
Declaration
public static string Left(this string this, int length)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
Int32 | length | 指定长度 不能小于 0 |
Returns
Type | Description |
---|---|
String |
Examples
"1234567890".Left(3); // "123"
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
RegexIsMatch(String, String)
正则表达式 - 是否匹配
使用None
Declaration
public static bool RegexIsMatch(this string this, string pattern)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
Returns
Type | Description |
---|---|
Boolean |
Examples
var source = "abcdefg\r\nabcdefghijk";
source.RegexIsMatch(@"^abc.*\r$", RegexOptions.Multiline); // true
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException | |
RegexMatchTimeoutException |
RegexIsMatch(String, String, RegexOptions)
正则表达式 - 是否匹配
Declaration
public static bool RegexIsMatch(this string this, string pattern, RegexOptions regexOption)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
RegexOptions | regexOption | 正则表达式选项 |
Returns
Type | Description |
---|---|
Boolean |
Examples
var source = "abcdefg\r\nabcdefghijk";
source.RegexIsMatch(@"^abc.*\r$", RegexOptions.Multiline); // true
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException | |
RegexMatchTimeoutException |
RegexMatch(String, String)
正则表达式 - 单个匹配对象
使用 None
Declaration
public static Match RegexMatch(this string this, string pattern)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
Returns
Type | Description |
---|---|
Match | 单个匹配对象 |
Examples
var str = "aaaabaaaabaaaa";
var match = str.RegexMatch("b"); // match.Success == true;
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException | |
RegexMatchTimeoutException |
RegexMatch(String, String, RegexOptions)
正则表达式 - 单个匹配对象
Declaration
public static Match RegexMatch(this string this, string pattern, RegexOptions regexOption)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
RegexOptions | regexOption | 正则表达式选项 |
Returns
Type | Description |
---|---|
Match | 单个匹配对象 |
Examples
var str = "aaaabaaaabaaaa";
var match = str.RegexMatch("b"); // match.Success == true;
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException | |
RegexMatchTimeoutException |
RegexMatches(String, String)
正则表达式 - 所有匹配对象
使用 None
Declaration
public static MatchCollection RegexMatches(this string this, string pattern)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
Returns
Type | Description |
---|---|
MatchCollection | 所有匹配对象 |
Examples
var str = "aaaabaaaabaaaa";
var matches = str.RegexMatches(@"aaaab"); // matches.Count == 2
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException |
RegexMatches(String, String, RegexOptions)
正则表达式 - 所有匹配对象
Declaration
public static MatchCollection RegexMatches(this string this, string pattern, RegexOptions regexOption)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
RegexOptions | regexOption | 正则表达式选项 |
Returns
Type | Description |
---|---|
MatchCollection | 所有匹配对象 |
Examples
var str = "aaaabaaaabaaaa";
var matches = str.RegexMatches(@"aaaab"); // matches.Count == 2
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException |
RegexReplace(String, String, String)
正则表达式 - 替换字符串
使用 None
Declaration
public static string RegexReplace(this string this, string pattern, string replacement)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
String | replacement | 替换字符串 |
Returns
Type | Description |
---|---|
String |
RegexReplace(String, String, String, RegexOptions)
正则表达式 - 替换字符串
Declaration
public static string RegexReplace(this string this, string pattern, string replacement, RegexOptions regexOption)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
String | replacement | 替换字符串 |
RegexOptions | regexOption | 正则表达式选项 |
Returns
Type | Description |
---|---|
String |
RegexReplace(String, String, MatchEvaluator)
正则表达式 - 替换字符串
Declaration
public static string RegexReplace(this string this, string pattern, MatchEvaluator evaluator)
Parameters
Type | Name | Description |
---|---|---|
String | this | 源字符串 |
String | pattern | 正则表达式 |
MatchEvaluator | evaluator | 委托表示的方法返回的字符串 |
Returns
Type | Description |
---|---|
String |
Examples
var rs2 = test1.RegexReplace(@"\.", match => match.Value + "@"); // "aaaa.@bbbb.@cccc"
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
RegexMatchTimeoutException |
RegexSplit(String, String)
正则表达式 - 拆分字符串
使用 None
Declaration
public static string[] RegexSplit(this string this, string pattern)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
Returns
Type | Description |
---|---|
String[] |
Examples
var str = "aaaa.bbbb.cccc";
var rs = str.RegexSplit(@"\."); // rs.Count == 3
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException | |
RegexMatchTimeoutException |
RegexSplit(String, String, RegexOptions)
正则表达式 - 拆分字符串
Declaration
public static string[] RegexSplit(this string this, string pattern, RegexOptions regexOption)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | pattern | 正则表达式 |
RegexOptions | regexOption | 正则表达式选项 |
Returns
Type | Description |
---|---|
String[] |
Examples
var str = "aaaa.bbbb.cccc";
var rs = str.RegexSplit(@"\."); // rs.Count == 3
Exceptions
Type | Condition |
---|---|
ArgumentException | |
ArgumentNullException | |
ArgumentOutOfRangeException | |
RegexMatchTimeoutException |
RemovePrefix(String, String, StringComparison)
移除前缀,若前缀不符则返回原字符串
默认不区分大小写
Declaration
public static string RemovePrefix(this string this, string prefix, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | prefix | 前缀 |
StringComparison | stringComparison |
Returns
Type | Description |
---|---|
String |
Examples
"t_table".RemovePrefix("T_"); // "table"
"t_table".RemovePrefix("T_", StringComparison.Ordinal); // "t_table"
"t_table".RemovePrefix("aaaaaaaaaaa", StringComparison.Ordinal); // "t_table"
"t_table".RemovePrefix("T_TABLE"); // ""
"t_table".RemovePrefix(null); // throw ArgumentNullException
default(string).RemovePrefix("ddd"); // throw ArgumentNullException
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
RemoveSuffix(String, String, StringComparison)
移除后缀,若后缀不符则返回原字符串
默认不区分大小写
Declaration
public static string RemoveSuffix(this string this, string suffix, StringComparison stringComparison = StringComparison.OrdinalIgnoreCase)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
String | suffix | 后缀 |
StringComparison | stringComparison |
Returns
Type | Description |
---|---|
String |
Examples
"logo.jpg".RemoveSuffix(".JPg"); // "logo"
"logo.jpg".RemoveSuffix(".JPG", StringComparison.Ordinal); // "logo.jpg"
"logo.jpg".RemoveSuffix("aaaaaaaa"); // "logo.jpg"
"logo.jpg".RemoveSuffix("LoGO.jpG"); // ""
"logo.jpg".RemoveSuffix(null); // throw ArgumentNullException
default(string).RemoveSuffix("dddd"); // throw ArgumentNullException
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
Right(String, Int32)
从右侧返回指定长度的字符串
Declaration
public static string Right(this string this, int length)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
Int32 | length | 指定长度 不能小于 0 |
Returns
Type | Description |
---|---|
String |
Examples
"1234567890".Right(3); // "890"
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
ToBytes(String)
转换为 byte 数组
编码格式 默认为 UTF8
Declaration
public static byte[] ToBytes(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
Byte[] |
Examples
var source = "abcd";
var rs = source.ToBytes(Encoding.Unicode);
var rs2 = source.ToBytes(); // use Encoding.UTF8
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
EncoderFallbackException |
ToBytes(String, Encoding)
转换为 byte 数组
Declaration
public static byte[] ToBytes(this string this, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
String | this | |
Encoding | encoding | 编码格式 |
Returns
Type | Description |
---|---|
Byte[] |
Examples
var source = "abcd";
var rs = source.ToBytes(Encoding.Unicode);
var rs2 = source.ToBytes(); // use Encoding.UTF8
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
EncoderFallbackException |
ToXmlDocument(String)
转换为 XmlDocument
Declaration
public static XmlDocument ToXmlDocument(this string this)
Parameters
Type | Name | Description |
---|---|---|
String | this |
Returns
Type | Description |
---|---|
XmlDocument |
Examples
var source = "<item><name>wrench</name></item>";
var rs = source.ToXmlDocument();
Exceptions
Type | Condition |
---|---|
XmlException |