473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Pad Middle of string with zeros

Ben
Hi

I have a string which is at least 4 characters but can be more which I need
to pad, with zeros, to a full length of 15 after the 3rd charactor.

Eg '1234' to '123000000000004'

How can I do this?

Also how can I truncate this back to 4 or however many charactors (e.g.
123000000000400 = 123400)

Any help will be much appreciated

Thanks
B
Nov 21 '05 #1
10 2599

"Ben" <Be*@NoSpam.com> schrieb im Newsbeitrag
news:up**************@TK2MSFTNGP12.phx.gbl...
Hi

I have a string which is at least 4 characters but can be more which I need to pad, with zeros, to a full length of 15 after the 3rd charactor.

Eg '1234' to '123000000000004'

How can I do this?
1. split the string into "123" and "4" (check out String.Substring())
2. pad the second string (check out String.PadLeft())
3. concatenate the two string (check out String.Concat())

Also how can I truncate this back to 4 or however many charactors (e.g.
123000000000400 = 123400)


remove the 4th character from your string (check out String.Remove()) while
it equals "0" (while loop with String.Equals()).

If you want to improve your knowledge on algorithms, i suggest the books by
Robert Sedgewick.
Nov 21 '05 #2
Diana,

"Diana Mueller" <diana@nospam> schrieb:
3. concatenate the two string (check out String.Concat())
I suggest to use the '&' operator for string concatenation instead of using
'String.Concat'.
If you want to improve your knowledge on algorithms, i suggest the books
by
Robert Sedgewick.


Full ACK!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Diana,

"Diana Mueller" <diana@nospam> schrieb:
3. concatenate the two string (check out String.Concat())
I suggest to use the '&' operator for string concatenation instead of using
'String.Concat'.
If you want to improve your knowledge on algorithms, i suggest the books
by
Robert Sedgewick.


Full ACK!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb im Newsbeitrag
news:OS**************@TK2MSFTNGP14.phx.gbl...
I suggest to use the '&' operator for string concatenation instead of using 'String.Concat'.


Why is that preferable?
Nov 21 '05 #5
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb im Newsbeitrag
news:OS**************@TK2MSFTNGP14.phx.gbl...
I suggest to use the '&' operator for string concatenation instead of using 'String.Concat'.


Why is that preferable?
Nov 21 '05 #6
Diana,

"Diana Mueller" <diana@nospam> schrieb:
I suggest to use the '&' operator for string concatenation instead of

using
'String.Concat'.


Why is that preferable?


Readability is better when using '&':

\\\
Dim s As String =
"Hello " & Name & "," & ControlChars.NewLine
///

vs.

\\\
Dim s As String = _
String.Concat("Hello ", Name, ",", ControlChars.NewLine)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7
Diana,

"Diana Mueller" <diana@nospam> schrieb:
I suggest to use the '&' operator for string concatenation instead of

using
'String.Concat'.


Why is that preferable?


Readability is better when using '&':

\\\
Dim s As String =
"Hello " & Name & "," & ControlChars.NewLine
///

vs.

\\\
Dim s As String = _
String.Concat("Hello ", Name, ",", ControlChars.NewLine)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8
Diana,
In addition to readability as Herfried suggests, the & operator will
concatenate any constants at compile time. String.Concat is strictly a
runtime function.

Const format As String = "Hello" & ControlChars.NewLine & "World"

When dealing with Object variables (parameters really) & Option Strict On
String.Concat is useful:

Option Strict On
Dim i1 As Object = 1
Dim f1 As Object = 1.0F
Dim d1 As Object = 100D
Dim s1 As Object = "help"

Debug.WriteLine(i1 & f1 & d1 & s1)
Debug.WriteLine(String.Concat(i1, f1, d1, s1))

The first statement is a compile error, while the second succeeds, both
should return the same result! As String.Concat is overloaded for both
String & Object!

Hope this helps
Jay

"Diana Mueller" <diana@nospam> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb im
Newsbeitrag
news:OS**************@TK2MSFTNGP14.phx.gbl...
I suggest to use the '&' operator for string concatenation instead of

using
'String.Concat'.


Why is that preferable?

Nov 21 '05 #9
Diana,
In addition to readability as Herfried suggests, the & operator will
concatenate any constants at compile time. String.Concat is strictly a
runtime function.

Const format As String = "Hello" & ControlChars.NewLine & "World"

When dealing with Object variables (parameters really) & Option Strict On
String.Concat is useful:

Option Strict On
Dim i1 As Object = 1
Dim f1 As Object = 1.0F
Dim d1 As Object = 100D
Dim s1 As Object = "help"

Debug.WriteLine(i1 & f1 & d1 & s1)
Debug.WriteLine(String.Concat(i1, f1, d1, s1))

The first statement is a compile error, while the second succeeds, both
should return the same result! As String.Concat is overloaded for both
String & Object!

Hope this helps
Jay

"Diana Mueller" <diana@nospam> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb im
Newsbeitrag
news:OS**************@TK2MSFTNGP14.phx.gbl...
I suggest to use the '&' operator for string concatenation instead of

using
'String.Concat'.


Why is that preferable?

Nov 21 '05 #10
Ben
Thanks for the posts

Sorry for the delayed response

Thanks
B
"Diana Mueller" <diana@nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

"Ben" <Be*@NoSpam.com> schrieb im Newsbeitrag
news:up**************@TK2MSFTNGP12.phx.gbl...
Hi

I have a string which is at least 4 characters but can be more which I

need
to pad, with zeros, to a full length of 15 after the 3rd charactor.

Eg '1234' to '123000000000004'

How can I do this?


1. split the string into "123" and "4" (check out String.Substring())
2. pad the second string (check out String.PadLeft())
3. concatenate the two string (check out String.Concat())

Also how can I truncate this back to 4 or however many charactors (e.g.
123000000000400 = 123400)


remove the 4th character from your string (check out String.Remove())
while
it equals "0" (while loop with String.Equals()).

If you want to improve your knowledge on algorithms, i suggest the books
by
Robert Sedgewick.

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul E Collins | last post by:
Given a string variable (form input), how can I determine whether it represents a valid integer? is_numeric is true for floats as well as integers, and is_int always fails on a string. P.
4
by: __PPS__ | last post by:
Hello, I have a function that does some decoding (same as urldecode in php) and it returns std::string std::string urldecode(const char* in, std::size_t length); so, this function parses input...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
10
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
4
by: Opa | last post by:
I have a string with n number of characters. I'm trying to create another string which is exactly 12 characters having leadings zeros and the input string at the end ex: string1 = "7344", I want...
1
by: Ben | last post by:
Hi I have a string which is at least 4 characters but can be more which I need to pad, with zeros, to a full length of 15 after the 3rd charactor. Eg '1234' to '123000000000004' How can I...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
6
by: Rich Raffenetti | last post by:
How can one format an integer into a hex string with leading zeros? Suppose an integer is 512 which in Hex is 200. I wish to print the 4-byte integer as 0200 or even 0x0200. The HEX function...
11
by: ram23 | last post by:
Hi friends, i need help .. i am getting String value and i need to add leading zeros.. total length of Target string is 13char eg: suppose if i get 12 as values.. it needs to convert into...
2
by: =?Utf-8?B?SExvbmc=?= | last post by:
Could someone give a hand on how to handle this issue: I need to find a way of removing zeros from files names. The zeros in the names are used to sort the file name, e.g. MAK0080.pci,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.