Solved

Format a string VB.Net, VS2005

Posted on 2008-06-12
6
229 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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