温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET中怎么实现字符串转义

发布时间:2021-07-19 16:05:42 来源:亿速云 阅读:192 作者:Leah 栏目:编程语言

本篇文章为大家展示了VB.NET中怎么实现字符串转义,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

C#中可以写

  1. string s = "This is a 
    string with newline.\n"

而VB.NET字符串转义的只能写

  1. Dim s = "This is a string 
    with newline."
     & vbLf 

人们渴望一个和C#中的"@"字符串正好相反的语法:

  1. string s = @"This is a string 
    with '\n' literal.\n";Dim s = 
    @"This is a string with newline.\n" 

但是,这种VB.NET字符串转义语法还没有被加入。

于是,我通过使用扩展函数,实现了比较接近的语法。

  1. Dim s = "This is a string 
    with newline.\n".Descape 

另外,还对String.Format进行了类似处理

  1. Dim s2 = "This is a 
    string that is {0}".
    Formats("formated.") 

VB.NET字符串转义的具体实现如下:

  1. '  

  2. ' File: StringDescape.vb  

  3. ' Description: VB.Net字符串转义语法糖 
    < Visual Basic 9> 

  4. ' Version: 2008.09.28.  

  5. ' (cc) F.R.C. 按照 Creative 
    Commons Public Domain Dedication L
    icense 捐献  

  6. ' http://creativecommons.org/
    licenses/publicdomain/  

Imports System  Imports System.Collections.Generic  Imports System.Text  Imports System.Text.RegularExpressions  Imports System.Runtime.CompilerServices  Imports Microsoft.VisualBasic
  1. /**/''' < summary>字符串转义< /summary> 

  2. Public Module StringDescapeModule StringDescape  

  3. /**/''' < summary>字符串反转义函数< /summary> 

  4. ''' < remarks> 

  5. ''' \0 与null \u0000 匹配  

  6. ''' \a 与响铃(警报)\u0007 匹配   

  7. ''' \b 与退格符 \u0008 匹配  

  8. ''' \t 与 Tab 符 \u0009 匹配   

  9. ''' \r 与回车符 \u000D 匹配  

  10. ''' \v 与垂直 Tab 符 \u000B 匹配  

  11. ''' \f 与换页符 \u000C 匹配  

  12. ''' \n 与换行符 \u000A 匹配  

  13. ''' \e 与 Esc 符 \u001B 匹配  

  14. ''' \x?? 与 \u00?? 匹配  

  15. ''' \u???? 与对应的Unicode字符对应  

  16. ''' < /remarks> 

  17. < Extension()> Public Function Descape
    ()Function Descape(ByVal This As 
    String) As String  

  18. Dim m = r.Match(This)  

  19. If Not m.Success Then Throw New 
    InvalidCastException 

  1. Dim ss As New SortedList(Of
     Integer, String)  

  2. For Each c As Capture In m.Groups.
    Item("SingleEscape").Captures  

  3. ss.Add(c.Index, SingleEscapeDict
    (c.Value))  

  4. Next  

  5. For Each c As Capture In m.Groups.
    Item("UnicodeEscape").Captures  

  6. ss.Add(c.Index, ChrW(CInt("&H" 
    & c.Value)))  

  7. Next  

  8. For Each c As Capture In m.Groups.
    Item("ErrorEscape").Captures  

  9. Throw New ArgumentException("
    ErrorEscape: Ch " & (c.Index + 1) 
    & " " & c.Value)  

  10. Next  

  11. For Each c As Capture In m.Groups.
    Item("Normal").Captures  

  12. ss.Add(c.Index, c.Value)  

  13. Next  

  14. Dim sb As New StringBuilder  

  15. For Each s In ss.Values  

  16. sb.Append(s)  

  17. Next  

  18. Return sb.ToString  

  19. End Function 

  1. /**/''' < summary>将指定的 String 
    中的格式项替换为指定的 Object 实例的值
    的文本等效项。< /summary> 

  2. < Extension()> Public Function Formats
    ()Function Formats(ByVal This As String, 
    ByVal arg0 As Object) As String  

  3. Return String.Format(This, arg0)  

  4. End Function  

  5. /**/''' < summary>将指定的 String 
    中的格式项替换为两个指定的 Object 实例的
    值的文本等效项。< /summary> 

  6. < Extension()> Public Function Formats
    ()Function Formats(ByVal This As String,
     ByVal arg0 As Object, ByVal arg1 As
     Object) As String  

  7. Return String.Format(This, arg0, arg1)  

  8. End Function  

  9. /**/''' < summary>将指定的 String 中的
    格式项替换为三个指定的 Object 实例的值的文本
    等效项。< /summary> 

  10. < Extension()> Public Function Formats
    ()Function Formats(ByVal This As String, 
    ByVal arg0 As Object, ByVal arg1 As Object, 
    ByVal arg2 As Object) As String  

  11. Return String.Format(This, arg0, arg1, arg2)  

  12. End Function  

  13. /**/''' < summary>将指定 String 中的格式
    项替换为指定数组中相应 Object 实例的值的文
    本等效项。< /summary> 

  14. < Extension()> Public Function Formats()
    Function Formats(ByVal This As String, 
    ByVal ParamArray args As Object()) As String  

  15. Return String.Format(This, args)  

  16. End Function  

  17. /**/''' < summary>将指定 String 中的格式项
    替换为指定数组中相应 Object 实例的值的文本等效项。
    指定的参数提供区域性特定的格式设置信息。< /summary> 

  18. < Extension()> Public Function Formats()Function 
    Formats(ByVal This As String, ByVal provider 
    As IFormatProvider, ByVal ParamArray args 
    As Object()) As String  

  19. Return String.Format(provider, This, args)  

  20. End Function 


  1. Private ReadOnly Property SingleEscapeDict()
    Property SingleEscapeDict() As Dictionary
    (Of String, String)  

  2. Get  

  3. Static d As Dictionary(Of String, String)  

  4. If d IsNot Nothing Then Return d  

  5. d = New Dictionary(Of String, String)  

  6. d.Add("\", "\") 'backslash  

  7. d.Add("0", ChrW(0)) 'null  

  8. d.Add("a", ChrW(7)) 'alert (beep)  

  9. d.Add("b", ChrW(8)) 'backspace  

  10. d.Add("f", ChrW(&HC)) 'form feed  

  11. d.Add("n", ChrW(&HA)) 'newline (lf)  

  12. d.Add("r", ChrW(&HD)) 'carriage return (cr)   

  13. d.Add("t", ChrW(9)) 'horizontal tab   

  14. d.Add("v", ChrW(&HB)) 'vertical tab  

  15. Return d  

  16. End Get  

  17. End Property  

  18. Private ReadOnly Property SingleEscapes
    ()Property SingleEscapes() As String  

  19. Get  

  20. Static s As String  

  21. If s IsNot Nothing Then Return s  

  22. Dim Chars As New List(Of String)  

  23. For Each c In "\0abfnrtv"  

  24. Chars.Add(Regex.Escape(c))  

  25. Next  

  26. s = "\\(?< SingleEscape>" & String.
    Join("|", Chars.ToArray) & ")"  

  27. Return s  

  28. End Get  

  29. End Property  

  30. Private UnicodeEscapes As String = 
    "\\[uU](?< UnicodeEscape>[0-9A-Fa-f]{4})
    |\\x(?< UnicodeEscape>[0-9A-Fa-f]{2})" 

  31. Private ErrorEscapes As String = 
    "(?< ErrorEscape>\\)" 

  32. Private Normal As String = "(?< Normal>.)" 

  33. Private r As New Regex("^" & "(
    " & SingleEscapes & "|" & Unicode
    Escapes & "|" & ErrorEscapes & "|" & 
    Normal & ")*" & "$", RegexOptions.
    ExplicitCapture)  

  34. End Module 

上述内容就是VB.NET中怎么实现字符串转义,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI