Click here to Skip to main content
11,718,016 members (82,467 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: string VB.NET
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 29-Jan-12 21:19pm
Edited 29-Jan-12 21:20pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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))
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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})

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 215
1 F-ES Sitecore 210
2 Maciej Los 140
3 Jochen Arndt 120
4 Andy Lanng 115
0 Sergey Alexandrovich Kryukov 1,649
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 30 Jan 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100