13,085,936 members (88,188 online)
Rate this:
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?
Posted 29-Jan-12 21:19pm
Updated 29-Jan-12 21:20pm
v2

Rate this:

## Solution 2

Try:
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))
Rate this:

## Solution 1

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[^],

A more straightforward way would be using string.Substring four times, followed by 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
Rate this:

## Solution 3

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)

Top Experts
Last 24hrsThis month
 Graeme_Grant 170 OriginalGriff 143 Jochen Arndt 140 Kornfeld Eliyahu Peter 140 BillWoodruff 80
 OriginalGriff 3,095 Graeme_Grant 1,519 ProgramFOX 1,367 ppolymorphe 1,217 Jochen Arndt 1,205