虽然说VB.NET中有很多东西是其他语言没有的,但是同时其他语言具有的一些特点,这款编程语言也是不具备的。不过我们可以通过其他的方式来实现。比如,众所周知,VB中没有C#的字符串转义语法。#t#
榆树ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
C#中可以写
而VB.NET字符串转义的只能写
人们渴望一个和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 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 中的格式
项替换为指定数组中相应 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 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字符串转义这段代码编写方法。
本文名称:VB.NET字符串转义详细概念解析
文章转载:http://www.shufengxianlan.com/qtweb/news3/524903.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联