成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

VB.NET字符串轉義詳細概念解析

開發 后端
我們在這里為大家帶來的一段代碼示例就是有關VB.NET字符串轉義的實現方法。下面就一起來詳細的解讀這段代碼的具體編寫方法。

雖然說VB.NET中有很多東西是其他語言沒有的,但是同時其他語言具有的一些特點,這款編程語言也是不具備的。不過我們可以通過其他的方式來實現。比如,眾所周知,VB中沒有C#的字符串轉義語法。#t#

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/  

 

  1. Imports System  
  2. Imports System.Collections.Generic  
  3. Imports System.Text  
  4. Imports System.Text.RegularExpressions  
  5. Imports System.Runtime.CompilerServices  
  6. 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字符串轉義這段代碼編寫方法。

責任編輯:曹凱 來源: 博客園
相關推薦

2010-01-21 18:08:25

VB.NET程序結構

2009-10-16 13:04:57

VB.NET字符串數組

2010-01-19 15:08:18

VB.NET對象成員

2010-01-11 18:46:15

VB.NET修飾符

2009-11-10 12:06:17

VB.NET字符串函數

2010-01-13 15:12:04

VB.NET字符串合并

2010-01-11 13:42:20

VB.NET字符串加密

2010-01-13 16:26:57

VB.NET通知控件

2010-01-14 14:56:07

2009-10-28 09:55:29

VB.NET MyCl

2010-01-13 10:15:19

VB.NET Stri

2010-01-08 14:56:18

VB.NET類定義

2010-01-12 18:05:38

VB.NET對象

2009-10-09 15:59:41

VB.NET對象

2009-10-10 16:44:52

VB.NET開發控件

2009-11-10 12:42:47

VB.NET Prin

2010-01-19 10:12:39

VB.NET Butt

2010-01-21 10:48:18

VB.NET擴展方法

2009-10-26 14:06:03

2010-01-07 15:18:10

VB.NET常量
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲成人av在线 | 国产视频在线观看一区二区三区 | 亚洲视频一区 | 亚洲欧洲精品成人久久奇米网 | 99re在线视频 | 久久久久中文字幕 | 中国一级毛片免费 | 亚洲一区 中文字幕 | 视频在线h | 久久久久久亚洲精品 | 日韩欧美在线免费观看 | 九九热免费在线观看 | 在线免费观看成人 | 久久激情视频 | 亚洲不卡在线观看 | 久久大 | 成人a视频片观看免费 | 日批免费在线观看 | 日韩av在线不卡 | 欧美 日韩 在线播放 | 亚洲视频在线观看一区二区三区 | 在线视频中文字幕 | 精品久久国产老人久久综合 | 午夜午夜精品一区二区三区文 | 日韩欧美国产一区二区 | 久久人人爽人人爽人人片av免费 | 国产一级一片免费播放 | 亚洲成人动漫在线观看 | 久久久久无码国产精品一区 | 91精品国产91久久久久久不卞 | 高清欧美性猛交xxxx黑人猛交 | 中文字幕第90页 | 韩国毛片视频 | 欧美日韩一区二区三区四区五区 | 婷婷久久久久 | 一区二区激情 | 黄片毛片免费看 | 亚洲精品一区二区二区 | www.久草.com | 久热久草 | 九九av|