符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本篇文章为大家展示了VB.NET中怎么实现字符串转义,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
为企业提供成都网站建设、网站建设、网站优化、营销型网站建设、竞价托管、品牌运营等营销获客服务。成都创新互联公司拥有网络营销运营团队,以丰富的互联网营销经验助力企业精准获客,真正落地解决中小企业营销获客难题,做到“让获客更简单”。自创立至今,成功用技术实力解决了企业“网站建设、网络品牌塑造、网络营销”三大难题,同时降低了营销成本,提高了有效客户转化率,获得了众多企业客户的高度认可!
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字符串转义语法还没有被加入。
于是,我通过使用扩展函数,实现了比较接近的语法。
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 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 中的格式
项替换为指定数组中相应 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 中的格式项
替换为指定数组中相应 Object 实例的值的文本等效项。
指定的参数提供区域性特定的格式设置信息。< /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
上述内容就是VB.NET中怎么实现字符串转义,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。