本篇文章为大家展示了VB.NET中怎么实现字符串转义,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
C#中可以写
string s = "This is a
string with newline.\n";
而VB.NET字符串转义的只能写
Dim s = "This is a string
with newline." & vbLf
人们渴望一个和C#中的"@"字符串正好相反的语法:
string s = @"This is a string
with '\n' literal.\n";Dim s =
@"This is a string with newline.\n"
但是,这种VB.NET字符串转义语法还没有被加入。
于是,我通过使用扩展函数,实现了比较接近的语法。
Dim s = "This is a string
with newline.\n".Descape
另外,还对String.Format进行了类似处理
Dim s2 = "This is a
string that is {0}".
Formats("formated.")
VB.NET字符串转义的具体实现如下:
'
' File: StringDescape.vb
' Description: VB.Net字符串转义语法糖
< Visual Basic 9>' Version: 2008.09.28.
' (cc) F.R.C. 按照 Creative
Commons Public Domain Dedication L
icense 捐献' 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
/**/''' < summary>字符串转义< /summary>
Public Module StringDescapeModule StringDescape
/**/''' < summary>字符串反转义函数< /summary>
''' < remarks>
''' \0 与null \u0000 匹配
''' \a 与响铃(警报)\u0007 匹配
''' \b 与退格符 \u0008 匹配
''' \t 与 Tab 符 \u0009 匹配
''' \r 与回车符 \u000D 匹配
''' \v 与垂直 Tab 符 \u000B 匹配
''' \f 与换页符 \u000C 匹配
''' \n 与换行符 \u000A 匹配
''' \e 与 Esc 符 \u001B 匹配
''' \x?? 与 \u00?? 匹配
''' \u???? 与对应的Unicode字符对应
''' < /remarks>
< Extension()> Public Function Descape
()Function Descape(ByVal This As
String) As StringDim m = r.Match(This)
If Not m.Success Then Throw New
InvalidCastException
Dim ss As New SortedList(Of
Integer, String)For Each c As Capture In m.Groups.
Item("SingleEscape").Capturesss.Add(c.Index, SingleEscapeDict
(c.Value))Next
For Each c As Capture In m.Groups.
Item("UnicodeEscape").Capturesss.Add(c.Index, ChrW(CInt("&H"
& c.Value)))Next
For Each c As Capture In m.Groups.
Item("ErrorEscape").CapturesThrow New ArgumentException("
ErrorEscape: Ch " & (c.Index + 1)
& " " & c.Value)Next
For Each c As Capture In m.Groups.
Item("Normal").Capturesss.Add(c.Index, c.Value)
Next
Dim sb As New StringBuilder
For Each s In ss.Values
sb.Append(s)
Next
Return sb.ToString
End Function
/**/''' < summary>将指定的 String
中的格式项替换为指定的 Object 实例的值
的文本等效项。< /summary>< Extension()> Public Function Formats
()Function Formats(ByVal This As String,
ByVal arg0 As Object) As StringReturn String.Format(This, arg0)
End Function
/**/''' < summary>将指定的 String
中的格式项替换为两个指定的 Object 实例的
值的文本等效项。< /summary>< Extension()> Public Function Formats
()Function Formats(ByVal This As String,
ByVal arg0 As Object, ByVal arg1 As
Object) As StringReturn String.Format(This, arg0, arg1)
End Function
/**/''' < summary>将指定的 String 中的
格式项替换为三个指定的 Object 实例的值的文本
等效项。< /summary>< Extension()> Public Function Formats
()Function Formats(ByVal This As String,
ByVal arg0 As Object, ByVal arg1 As Object,
ByVal arg2 As Object) As StringReturn String.Format(This, arg0, arg1, arg2)
End Function
/**/''' < summary>将指定 String 中的格式
项替换为指定数组中相应 Object 实例的值的文
本等效项。< /summary>< Extension()> Public Function Formats()
Function Formats(ByVal This As String,
ByVal ParamArray args As Object()) As StringReturn String.Format(This, args)
End Function
/**/''' < summary>将指定 String 中的格式项
替换为指定数组中相应 Object 实例的值的文本等效项。
指定的参数提供区域性特定的格式设置信息。< /summary>< Extension()> Public Function Formats()Function
Formats(ByVal This As String, ByVal provider
As IFormatProvider, ByVal ParamArray args
As Object()) As StringReturn String.Format(provider, This, args)
End Function
Private ReadOnly Property SingleEscapeDict()
Property SingleEscapeDict() As Dictionary
(Of String, String)Get
Static d As Dictionary(Of String, String)
If d IsNot Nothing Then Return d
d = New Dictionary(Of String, String)
d.Add("\", "\") 'backslash
d.Add("0", ChrW(0)) 'null
d.Add("a", ChrW(7)) 'alert (beep)
d.Add("b", ChrW(8)) 'backspace
d.Add("f", ChrW(&HC)) 'form feed
d.Add("n", ChrW(&HA)) 'newline (lf)
d.Add("r", ChrW(&HD)) 'carriage return (cr)
d.Add("t", ChrW(9)) 'horizontal tab
d.Add("v", ChrW(&HB)) 'vertical tab
Return d
End Get
End Property
Private ReadOnly Property SingleEscapes
()Property SingleEscapes() As StringGet
Static s As String
If s IsNot Nothing Then Return s
Dim Chars As New List(Of String)
For Each c In "\0abfnrtv"
Chars.Add(Regex.Escape(c))
Next
s = "\\(?< SingleEscape>" & String.
Join("|", Chars.ToArray) & ")"Return s
End Get
End Property
Private UnicodeEscapes As String =
"\\[uU](?< UnicodeEscape>[0-9A-Fa-f]{4})
|\\x(?< UnicodeEscape>[0-9A-Fa-f]{2})"Private ErrorEscapes As String =
"(?< ErrorEscape>\\)"Private Normal As String = "(?< Normal>.)"
Private r As New Regex("^" & "(
" & SingleEscapes & "|" & Unicode
Escapes & "|" & ErrorEscapes & "|" &
Normal & ")*" & "$", RegexOptions.
ExplicitCapture)End Module
上述内容就是VB.NET中怎么实现字符串转义,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。