Show / Hide Table of Contents

    Class StringExtensions

    string 扩展方法

    Inheritance
    Object
    StringExtensions
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: Lett.Extensions
    Assembly: Lett.Extensions.dll
    Syntax
    public static class StringExtensions

    Methods

    | Improve this Doc View Source

    AppendPrefixIfNotExist(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

    this 不允许为空

    ArgumentNullException

    prefix 不允许为空

    | Improve this Doc View Source

    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

    this 不允许为空

    ArgumentNullException

    suffix 不允许为空

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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

    @this is null.

    ArgumentException

    @this is an empty string (""), contains only white space, or contains one or more invalid characters. -or- this refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.

    NotSupportedException

    @this refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in a non-NTFS environment.

    FileNotFoundException

    The file cannot be found, such as when fileMode is FileMode.Truncate or FileMode.Open, and the file specified by this does not exist. The file must already exist in these modes.

    IOException

    An I/O error, such as specifying FileMode.CreateNew when the file specified by this already exists, occurred. -or- The system is running Windows 98 or Windows 98 Second Edition and fileShare is set to FileShare.Delete. -or- The stream has been closed.

    SecurityException

    The caller does not have the required permission.

    DirectoryNotFoundException

    The specified path is invalid, such as being on an unmapped drive.

    UnauthorizedAccessException

    The fileAccess requested is not permitted by the operating system for the specified this, such as when fileAccess is Write or ReadWrite and the file or directory is set for read-only access.

    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.

    | Improve this Doc View Source

    Base64Decode(String)

    进行 BASE6 4解码

    使用 UTF8

    Declaration
    public static string Base64Decode(this string this)
    Parameters
    Type Name Description
    String this
    Returns
    Type Description
    String

    转换失败返回 null

    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

    转换失败返回 null

    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

    values is null

    ArgumentException
    | Improve this Doc View Source

    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

    values is null

    ArgumentException
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    IgnoreCaseEquals(String, String)

    忽略大小写比较

    使用 CurrentCultureIgnoreCase

    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
    | Improve this Doc View Source

    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)

    使用通配符比较字符串是否相似

    通配符 *

    特殊字符 用 \ 转义

    正则匹配时,使用 Singleline

    Declaration
    public static bool IsLike(this string this, string pattern)
    Parameters
    Type Name Description
    String this
    String pattern

    通配符表达式,为 null 时返回 false

    Returns
    Type Description
    Boolean

    this为 null 时 返回 false

    pattern为 null 时 返回 false

    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
    | Improve this Doc View Source

    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

    length 不能小于 0

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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

    this 不允许为空

    ArgumentNullException

    prefix 不允许为空

    | Improve this Doc View Source

    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

    this 不允许为空

    ArgumentNullException

    suffix 不允许为空

    | Improve this Doc View Source

    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

    length 不能小于 0

    | Improve this Doc View Source

    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

    this is null

    EncoderFallbackException
    | Improve this Doc View Source

    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

    encoding is null

    ArgumentNullException

    this is null

    EncoderFallbackException
    | Improve this Doc View Source

    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
    • Improve this Doc
    • View Source
    Back to top Copyright (c) 2019 viacooky. All rights reserved.