Convert string to special format

Hello,

I'm trying to convert the value of a textbox to something like this;

XXX XXX XXX XXXXXX

Where the value of the textbox can be numbers or letters, upper or lower case.

Example:

I enter 7m0343834ewrw and I want the result to be 7M0 343 834 EWRW.

How do I format MyTextBox?

thanks,

jppinto
LVL 33
jppintoAsked:
Who is Participating?
 
Jorge PaulinoIT Pro/DeveloperCommented:
If you want to use that way you need to change the Left() function to Microsoft.VisualBasic.Left() because otherwise you're using a Left() function from another namespace. This is a function that comes from older versions and you have to indicate the correct namespace for compatibility.
Here's a .NET way:

    ''' <summary>
    ''' Converts to a specific format
    ''' </summary>
    ''' <returns></returns>
    Function formatString(ByVal str As String) As String
        Try
 
            Dim part1 As String = str.Substring(0, 3).ToUpper
            Dim part2 As String = str.Substring(3, 3).ToUpper
            Dim part3 As String = str.Substring(6, 3).ToUpper
            Dim part4 As String = str.Substring(9).ToUpper
 
            Return String.Format("{0} {1} {2} {3}", part1, part2, part3, part4)
 
        Catch ex As Exception
            Return String.Empty
        End Try
 
    End Function
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String = "7m0343834ewrw"
        MessageBox.Show(formatString(str))
    End Sub

Open in new window

0
 
reb73Commented:
LEFT(MyTextBox.Text, 3) & " " & MID(MyTextBox.Text,4, 3) & " " & MID(MyTextBox.Text,7, 3) & " "  & MID(MyTextBox.Text,10,50)
0
 
Patrick MatthewsCommented:
In VB6...


Dim strInput As String

strInput = UCase(Replace(SomeForm.MyTextBox, " ", ""))
strInput = Trim(Left(strInput, 3) & " " & Mid(strInput, 4, 3) & " " & Mid(strInput, 7, 3) & " " & Right(strInput, 4))

MsgBox strInput
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Patrick MatthewsCommented:
Need more coffee :)
0
 
jppintoAuthor Commented:
Error:

Public Property Left() As Integer' has no parameters and its return type cannot be indexed.

jppinto
0
 
jppintoAuthor Commented:
Thanks jpaulino. Now it's working!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.