Convert ASCII Character Code to Character in VB.NET
I have a value I am pulling into a string that looks like this:
M'arta
I need to have it to translate the numeric value into an actual value so that the string looks like this:
M'arta
Any ideas on how to accomplish this in VB.NET? Here is the relevant line of code that returns this result:
Dim occupant as String = GridView1.Rows(e.RowIndex).Cells(2).Te开发者_如何转开发xt
Below is the VB (& C#) version to what you're asking. Basically, use the MatchEvaluator argument in the Regex method to allow custom parsing of the matches. In this case, we find any instances of #<1-3_digit_number>
we want to strip the `# symbol, and convert the decimal code.
I added a second conversion in your string (#116
) just for testing purposes. You could refactor this in to a custom method and (not sure if VB has it) lambda expression to make it universal, but I'll leave that up to you.
VB.NET Version (DEMO)
Imports System.Text.RegularExpressions
Public Class Test
Public Shared Sub Main()
Dim sample As [String] = "M#39ar#116a"
Dim reg As New Regex("\x23\d{1,3}")
Console.WriteLine(reg.Replace(sample, New MatchEvaluator(AddressOf ReplaceASCIICode)))
End Sub
Public Shared Function ReplaceASCIICode(m As Match) As [String]
Dim code As Int32 = Int32.Parse(m.Value.Substring(1))
Return Convert.ToString(ChrW(code))
End Function
End Class
C# Version (DEMO)
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
String sample = "M#39ar#116a";
Regex reg = new Regex(@"\x23\d{1,3}");
Console.WriteLine(reg.Replace(sample, new MatchEvaluator(ReplaceASCIICode)));
}
public static String ReplaceASCIICode(Match m)
{
Int32 code = Int32.Parse(m.Value.Substring(1));
return Convert.ToString((char)code);
}
}
For archival purposes, here are the versions that support &#___;
below:
VB.NET (DEMO)
Imports System.Text.RegularExpressions
Public Class Test
Public Shared Sub Main()
Dim sample As [String] = "M'arta"
Dim reg As New Regex("&#(\d{1,3});")
Console.WriteLine(reg.Replace(sample, New MatchEvaluator(AddressOf ReplaceASCIICode)))
End Sub
Public Shared Function ReplaceASCIICode(m As Match) As [String]
Dim code As Int32 = Int32.Parse(m.Groups(1).Value)
Return Convert.ToString(ChrW(code))
End Function
End Class
C# (DEMO)
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
String sample = "M'arta";
Regex reg = new Regex(@"&#(\d{1,3});");
Console.WriteLine(reg.Replace(sample, new MatchEvaluator(ReplaceASCIICode)));
}
public static String ReplaceASCIICode(Match m)
{
Int32 code = Int32.Parse(m.Groups[1].Value);
return Convert.ToString((char)code);
}
}
You can parse the string looking for the #digits and then put the found code through the ChrW .NET function.
If you don't know regex then something like this
Dim idx As Integer = occupant.IndexOf("#")
If idx <> -1 Then
idx += 1
Do While idx < occupant.Length
If IsNumeric(occupant(idx)) Then
s &= occupant(idx)
Else
Exit Do
End If
idx += 1
Loop
If s.Length > 0 Then
s = Convert.ToChar(CInt(s))
End If
Stop
End If
It would need slight modification to handle multiple instances of #.
I don't know VB.NET, but here is a C# solution. I am pretty sure you can handle the conversion to Visual Basic.
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
String input = "M#39arta";
String output = Regex.Replace(input, @"#\d\d", Replace);
Console.WriteLine(output);
Console.ReadLine();
}
static String Replace(Match match) {
int charCode = int.Parse(match.Value.Substring(1));
return ""+ (char)charCode;
}
}
精彩评论