473,794 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 '12300000000000 4'

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 2635

"Ben" <Be*@NoSpam.com > schrieb im Newsbeitrag
news:up******** ******@TK2MSFTN GP12.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 '12300000000000 4'

How can I do this?
1. split the string into "123" and "4" (check out String.Substrin g())
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******** ******@TK2MSFTN GP14.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******** ******@TK2MSFTN GP14.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.Ne wLine
///

vs.

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

--
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.Ne wLine
///

vs.

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

--
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.Ne wLine & "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******** ******@TK2MSFTN GP14.phx.gbl...
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schrieb im
Newsbeitrag
news:OS******** ******@TK2MSFTN GP14.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.Ne wLine & "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******** ******@TK2MSFTN GP14.phx.gbl...
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> schrieb im
Newsbeitrag
news:OS******** ******@TK2MSFTN GP14.phx.gbl...
I suggest to use the '&' operator for string concatenation instead of

using
'String.Concat' .


Why is that preferable?

Nov 21 '05 #10

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

Similar topics

6
6653
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
4259
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 and constructes output char by char. I tried to use all sorts of direct access to the buffer of std::string to be returned but it's always slow. It appered that if I dynamically allocate memory fill it with output, then create std::string from this...
17
14364
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, int longueur) { char *resultat = " "; char *temporaire = " "; int nbr;
10
43475
by: Yang Lee | last post by:
hi is this correct char teststr="\0"; or is there another method to initialise; thanks lee
4
1831
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 to format string1 such that it results another string with string1 at the end and leading zeros in front of it like: "000000007344 so I try string2=String.Format(??????,string1 does anyone know the correct formatting expression Thank
1
312
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 do this? Also how can I truncate this back to 4 or however many charactors (e.g.
6
7613
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, Karthi
6
19200
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 doesn't create leading zeros. The Format function (format(value,"X") doesn't create leading zeros. I believe there should be a simple way that doesn't involving measuring and padding the string.
11
45631
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 0000000000012
2
1228
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, MAK-0080.pci. What I need to do is to remove the zeros, so the file names become MAK80.pci o MAK-80.pci. I have done something in Visual Basic using the Mid() function and going thru the string removing the zeros. Is there a better way of doing this,...
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.