First of Hi peeps .... I have a small prob that I want to run by anyone who could help me simplify this code, Well I'm trying to change the string value (123A456B789) into a numerical value like this '123' 'A=0' '456' 'B=1' '789', but for the same reason as saving the time in writing 50 lines just to incorporate this function I need a quicker solution...

Here's my code but bare in mind I am still new at VB.NET and the code is only to change A and B to numerical values.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each ch As Char In TextBox1.Text
            If TextBox1.Text.Contains("A") Or TextBox1.Text.Contains("B") Then
                Dim chA As Char = "A"
                Dim chB As Char = "B"
 
                TextBox2.Text = TextBox1.Text.Replace(chA, Asc(chA))
                TextBox2.Text = TextBox2.Text.Replace(chB, Asc(chB))

            End If
        Next
    End Sub

I hope one of you could show me a quicker yet better way of resolving this issue.

@Spacenoobie

Welcome!

Sorry, I'm not quite following the problem. Do you want to split that string?

First i am confused how u get A=0 as A's ascii code is 65
Anyway below is the code to convert all letter to their ascii code.

Imports System.Text.RegularExpressions

Module Module1

	Sub Main()
		Dim str As String = "123A456B789"
		Dim reg As New Regex("[a-zA-Z]")
		Dim newString As String = reg.Replace(str, New MatchEvaluator(AddressOf ReplaceAlpha))
		Debug.WriteLine(newString)
		Console.Read()
	End Sub

	Public Function ReplaceAlpha(ByVal m As Match) As String
	        Return Asc( m.Value).ToString
	End Function

End Module

Return string = 1236545666789

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.