Solved

Format a string VB.Net, VS2005

Posted on 2008-06-12
6
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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