Click here to Skip to main content
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 22:19pm
Edited 29-Jan-12 22: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 Sergey Alexandrovich Kryukov 504
1 Maciej Los 349
2 Kornfeld Eliyahu Peter 325
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 Jan 2012
Copyright © CodeProject, 1999-2014
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