用零填充字符串中间 [英] Pad Middle of string with zeros
问题描述
嗨
我有一个至少4个字符的字符串但可以更多我需要
填充,用零填充,到一个完整的第3个字符后的长度为15个。
例如''1234''到''123000000000004''
我该怎么做?
另外如何将此截断为4或多个字符(例如
123000000000400 = 123400)
非常感谢任何帮助
谢谢
B
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
推荐答案
" Ben" < Be*@NoSpam.com> schrieb im Newsbeitrag
新闻:上传************** @ TK2MSFTNGP12.phx.gbl ...
"Ben" <Be*@NoSpam.com> schrieb im Newsbeitrag
news:up**************@TK2MSFTNGP12.phx.gbl...
嗨
我有一个至少4个字符的字符串,但可以更多,我需要在第3个字符后用零填充,全长15个字符。
例如''1234''到''123000000000004''
我该怎么做?
1.将字符串分成123字符串。和4 (查看String.Substring())
2.填充第二个字符串(查看String.PadLeft())
3.连接两个字符串(查看字符串.Concat())
另外如何将此截断为4或多个字符(例如
123000000000400 = 123400)
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)
从字符串中删除第4个字符(查看String.Remove())而
它等于0 (使用String.Equals()进行循环)。
如果你想提高你对算法的了解,我建议你用b / b
罗伯特塞奇威克的书。
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.
Diana,
Diana Mueller <黛安娜@ NOSPAM> schrieb:
Diana,
"Diana Mueller" <diana@nospam> schrieb:
3.连接两个字符串(检查String.Concat())
我建议使用''&''运算符字符串连接而不是使用
''String.Concat''。
如果你想提高你对算法的了解,我建议书籍
by br Robert Sedgewick。
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.
完全确认!
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet / faqs />
Full ACK!
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Diana,
" Diana Mueller" <黛安娜@ NOSPAM> schrieb:
Diana,
"Diana Mueller" <diana@nospam> schrieb:
3.连接两个字符串(检查String.Concat())
我建议使用''&''运算符字符串连接而不是使用
''String.Concat''。
如果你想提高你对算法的了解,我建议书籍
by br Robert Sedgewick。
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.
完全确认!
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet / faqs />
Full ACK!
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
这篇关于用零填充字符串中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!