Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format a string VB.Net, VS2005

Posted on 2008-06-12
6
Medium Priority
?
236 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 1200 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 400 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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