Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String manipulation

Status
Not open for further replies.

week

MIS
Feb 14, 2001
118
0
0
US
I have a variable that is something like "7". If I need it to be "007", how do I do that? I think there is some function to do this, isn't there? I am just not that familiar with the string manipulation in VB....thanks.
 
You can use the format function. i.e.
Code:
Private Sub Form_Load()
Dim strTest As String
strTest = 7
strTest = Format(strTest, "000")
MsgBox strTest
End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
If you are sure that "7" is a string you can use

myVar = "7"
myString = "00" & myVar

you can type case "7" to a string also if it is not

CStr(7)
PB
 
Well,

I need to give you guys few more info.

1. 7 is defined as Integer. c8amsm, would this be problem using your method?

2. 7 could be 17 or 117..meaning it could be one, two, or three digit. But my final output need to be in four digit. It doesn't have to be Integer...

Sorry for not making this clear at the beginning.

Thanks.
 
If it is defined as an an integer then yes it would be a problem as the integer value of 007 would be 7 and thisis what would be displayed.

I would consider making it a string or defining a second variable which is a string and using that one.

The code will work correctly regardless of whether it is 1,2 or 3 digits. Simply change the amount of 0's if you need any more adding.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Suppose terr is integer. Will this work? Unfortunately, I can't test this and hope this is correct before I put it out there.

Dim lcTerr As String
lcTerr = Format(terr, "0000")

Thanks again.
 
have a look at the String() and Len() functions
 
The code that ca8msm used will work.

Option Explicit

Private Sub Form_Load()
Dim intVar As Integer

intVar = 57

Label1.Caption = Format(intVar, "000")

End Sub

I hope this helps you.
 
The format function will take any data type. I use it for dispaly purposes because leading zero's are usually dropped by number variables anyway. Here is a typical funtion that you could use,

text1=Format(<myvariable>,&quot;###000&quot;)

This will ensure that youe value is aleways at least 3 charaters long. The &quot;#&quot; is a numeric placeholder that will not be displayed, but will allow a value up to 6 charaters (in this case). For example,

7 becomes &quot;007&quot;
17 becomes &quot;017&quot;
1117 becomes &quot;1117&quot;
etc.

Thanks and Good Luck!

zemp
 

Seems like alot of cross posts happenning...sorry people...
 
That happens when multiple responses are being done at the same time. On the other hand it's good to have consensus.

Thanks and Good Luck!

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top