在vb.net中格式化字符串 [英] Format string in vb.net

查看:143
本文介绍了在vb.net中格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长度为18的字符串,如下所示:-

123123452149856874

我想将该字符串格式化为8-4-4-2格式.
喜欢,

12312345 2149 8568 74

执行此操作的最短方法是什么?
请指导...

I have a string whose length is 18 like follow:-

123123452149856874

I want to format that string in 8-4-4-2 format.
like,

12312345 2149 8568 74

What is the shortest way to perform this action?
Please guide...

推荐答案

尝试:
Dim s As String = "123123452149856874"
Console.WriteLine("{0} {1} {2} {3}", s.Substring(0, 8), s.Substring(8, 4), s.Substring(12, 4), s.Substring(16, 2))


使用System.Text.RegularExpressions.Regex.Replace使用正则表达式(其中包含4个组)可以很容易地做到这一点,例如:
This is done pretty easily using System.Text.RegularExpressions.Regex.Replace using a Regular Expression with 4 groups in it like:
([0-9]{8})([0-9]{4})([0-9]{4})([0-9]{2})



请参阅:
http://msdn.microsoft.com/en-us/library/system. text.regularexpressions.regex.aspx [ ^ ],
http://www.nncron.ru/help/EN/add_info/regexp.htm [ ^ ].

一种更直接的方法是使用string.Substring四次,然后使用string.Format("{0} {1} {2} {3}", arrayOfFourSubstrings).

请参阅:
http://msdn.microsoft.com/en-us/library/aka44szs.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx [ ^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^],
http://www.nncron.ru/help/EN/add_info/regexp.htm[^].

A more straightforward way would be using string.Substring four times, followed by string.Format("{0} {1} {2} {3}", arrayOfFourSubstrings).

Please see:
http://msdn.microsoft.com/en-us/library/aka44szs.aspx[^],
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx[^].

—SA


您可以使用 String.Substring()函数
这是示例
You can use String.Substring() function
here is example
Dim myString As String = "123123452149856874"
Dim test1 As string = myString.Substring(1,8)
Dim test1 As string = myString.Substring(9,12)
Dim test1 As string = myString.Substring(13,16)
Dim test1 As string = myString.Substring(17,18)


这篇关于在vb.net中格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆