?
Solved

Convert string to special format

Posted on 2009-02-16
6
Medium Priority
?
182 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:jppinto
6 Comments
 
LVL 25

Expert Comment

by:reb73
ID: 23649693
LEFT(MyTextBox.Text, 3) & " " & MID(MyTextBox.Text,4, 3) & " " & MID(MyTextBox.Text,7, 3) & " "  & MID(MyTextBox.Text,10,50)
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 23649696
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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 23649698
Need more coffee :)
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 33

Author Comment

by:jppinto
ID: 23649836
Error:

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

jppinto
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 23650395
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
 
LVL 33

Author Closing Comment

by:jppinto
ID: 31547317
Thanks jpaulino. Now it's working!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question