Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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...
Posted
Updated 29-Jan-12 21:20pm
v2

Try:
VB
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))
 
Share this answer
 
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})


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
 
Share this answer
 
You can use String.Substring() function
here is example
VB
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)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900