VB.NET字符串轉(zhuǎn)義詳細概念解析
雖然說VB.NET中有很多東西是其他語言沒有的,但是同時其他語言具有的一些特點,這款編程語言也是不具備的。不過我們可以通過其他的方式來實現(xiàn)。比如,眾所周知,VB中沒有C#的字符串轉(zhuǎn)義語法。#t#
C#中可以寫
- string s = "This is a 
string with newline.\n"; 
而VB.NET字符串轉(zhuǎn)義的只能寫
- 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字符串轉(zhuǎn)義語法還沒有被加入。
于是,我通過使用擴展函數(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字符串轉(zhuǎn)義的具體實現(xiàn)如下:
- '
 - ' File: StringDescape.vb
 - ' Description: VB.Net字符串轉(zhuǎn)義語法糖
 
< 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>字符串轉(zhuǎn)義< /summary>
 - Public Module StringDescapeModule StringDescape
 - /**/''' < summary>字符串反轉(zhuǎn)義函數(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 String- Dim 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").Captures- ss.Add(c.Index, SingleEscapeDict
 
(c.Value))- Next
 - For Each c As Capture In m.Groups.
 
Item("UnicodeEscape").Captures- ss.Add(c.Index, ChrW(CInt("&H"
 
& c.Value)))- Next
 - For Each c As Capture In m.Groups.
 
Item("ErrorEscape").Captures- Throw New ArgumentException("
 
ErrorEscape: Ch " & (c.Index + 1)
& " " & c.Value)- Next
 - For Each c As Capture In m.Groups.
 
Item("Normal").Captures- ss.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 String- Return 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 String- Return 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 String- Return 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 String- Return 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 String- Return 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 String- Get
 - 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字符串轉(zhuǎn)義這段代碼編寫方法。















 
 
 
 
 
 
 