您好,登錄后才能下訂單哦!
本篇文章為大家展示了VB.NET中怎么實現(xiàn)字符串轉義,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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字符串轉義語法還沒有被加入。
于是,我通過使用擴展函數(shù),實現(xiàn)了比較接近的語法。
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字符串轉義的具體實現(xiàn)如下:
'
' 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>字符串反轉義函數(shù)< /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 中的格式
項替換為指定數(shù)組中相應 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 中的格式項
替換為指定數(shù)組中相應 Object 實例的值的文本等效項。
指定的參數(shù)提供區(qū)域性特定的格式設置信息。< /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
上述內(nèi)容就是VB.NET中怎么實現(xiàn)字符串轉義,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。