Solved

Format a string VB.Net, VS2005

Posted on 2008-06-12
6
226 Views
Last Modified: 2010-04-23
Hello experts,

I may be crazy, but I need to allow the user to input a selection of numbers in to a text box. I have provide directions to separte each by comma's, but you know how that goes.

This is much the same as the "Print range" in the common printer dialog box where you can put pages 1, 5, 8, 22

The output needs to be reformatted so that each entry is 4 digits (preceeding zero's if necessary) and separated by commas.

So, if the user puts in "1, 35, 400" the resulting string will be "0001,0035, 0400".
Basically seperating by the comma's, and then reformatting to four digits (pad left with zero?)

Less important, but I am also not sure if there is a way to account for other variations ("1 35 400",  "1-35/400" etc) and have the the resulting output string still be "0001,0035, 0400".

Thanks in advance,
webkiwi


0
Comment
Question by:webkiwi1
6 Comments
 
LVL 10

Accepted Solution

by:
bluefezteam earned 300 total points
ID: 21770959
This will do it
function padString(str)

	dim strLen, i

	strLen = len(str)

	for i = 0 to (3-strLen)

		str = "0" & str

	next

	padString = str

end function

dim strIn, arr, i, strOut

strIn = "1,5,8,22"

arr = split(strIn,",")
 

for i = 0 to uBound(arr)

	strOut &= padString(arr(i))

	if i < uBound(arr) then strOut &= ","

next
 

response.write strOut

Open in new window

0
 
LVL 19

Assisted Solution

by:elimesika
elimesika earned 100 total points
ID: 21771083
right , but there is a more elegant way ...
function padString(str)

	Dim n as Integer = Int32.Parse(str)

	str = n.ToString("0000")

        padString = str

end function

Open in new window

0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 21771714
this is my proposal, more .net stylished:

Function FormatString(str as String) as String
      Dim pages as String() = str.Split(New [Char]() {" "c})
      Dim I as Integer
      For i = 0 To pages.Length-1
            pages(i) = [Int32].Parse(pages(i)).ToString("0000")
      Next i
      Return [String].Join(",", pages)
End Function
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:webkiwi1
ID: 21772303
Thank you for the reponses. I have tried all three solutions and only bluefezteam seemed to work for me (not to say the others did not). The errors I got from both elimesika:and jaime_olivares:was "Input string in the wrong format" or similar...I think it was having problems with the comma.

Awarding points accordingly.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21772428
sorry, there is a bug in my code, this line:
Dim pages as String() = str.Split(New [Char]() {" "c})
     
should be:
Dim pages as String() = str.Split(New [Char]() {","c})
     
Hope you can test it.
0
 

Author Comment

by:webkiwi1
ID: 21773226
No problem, worked this time...I thought that may be the case.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expando 4 47
As soon as i click a button on the form,it mnimises. 11 30
I need help embedding an image as HTML in my vb.net application 3 50
vb.net checkbox 7 41
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now