bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

VB.NET中怎么實現字符串轉義

本篇文章為大家展示了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#中的"@"字符串正好相反的語法:

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字符串轉義的具體實現如下:

'  ' FileStringDescape.vb  ' Description: VB.Net字符串轉義語法糖 < Visual Basic 9> ' Version2008.09.28.  ' (cc) F.R.C. 按照 Creative Commons Public Domain Dedication License 捐獻  ' 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 String  Dim m = r.Match(This)  If Not m.Success Then Throw New InvalidCastException 
Dim ss As New SortedList(Of IntegerString)  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 StringByVal arg0 As ObjectByVal arg1 As ObjectByVal arg2 As ObjectAs String  Return 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 String  Return String.Format(This, args)  End Function  /**/''' < summary>將指定 String 中的格式項替換為指定數組中相應 Object 實例的值的文本等效項。指定的參數提供區域性特定的格式設置信息。< /summary> < Extension()> Public Function Formats()Function Formats(ByVal This As StringByVal provider As IFormatProviderByVal 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 & "|" & UnicodeEscapes & "|" & ErrorEscapes & "|" & Normal & ")*" & "$", RegexOptions.ExplicitCapture)  End Module 

上述內容就是VB.NET中怎么實現字符串轉義,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。

分享標題:VB.NET中怎么實現字符串轉義
當前路徑:http://vcdvsql.cn/article22/gjiijc.html

成都網站建設公司_創新互聯,為您提供網站策劃網站設計網站制作企業建站建站公司軟件開發

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司