Solved

Format a string VB.Net, VS2005

Posted on 2008-06-12
6
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:webkiwi1
Comment Utility
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
Comment Utility
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
Comment Utility
No problem, worked this time...I thought that may be the case.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

16 Experts available now in Live!

Get 1:1 Help Now