Split an integer to create ':' separated string

Hi Experts,

Party politics and partly the other dev doesn't want to change the data format he is sending I've been left with this problem.

I have an integer made up of 3 parts for example: 131110 I need to display this as 13:11:10.

How do I efficiently split this using asp.net VB.

This is further complicated by having to drop any leading zeros so:

90807 needs to show as 9:8:7 (the data width is 6, so assuming the left most digit to be 09)
081207 shows as 8:12:7
etc

Can anyone point me in the right direction please.

Andy
LVL 3
Andy GreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Something like this - pulling from right to left...

myData = 90807

rightMost = right(myData,2)
middle=RIGHT(LEFT(G11,LEN(G11)-2),2)
leftOver = =RIGHT(LEFT(G11,LEN(G11)-4),2)

newData = leftOver & ":" & middle & ":" & rightMost

Open in new window

0
it_saigeDeveloperCommented:
Not really too difficult if you can guarantee a field width of 6.  Here are three different method (two are really the same thing just differing implementations):
Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Module Module1
	Dim _sets As String() = {"131110", "090807"}
	Sub Main()
		For Each _set In _sets
			Console.WriteLine("Using For")
			Console.WriteLine(ParseDataUsingFor(_set))
			Console.WriteLine("Using Linq")
			Console.WriteLine(ParseDataUsingLinq(_set))
			Console.WriteLine("Using RegEx")
			Console.WriteLine(ParseDataUsingRegEx(_set))
		Next
		Console.ReadLine()
	End Sub
End Module

Module Extensions
	<Extension>
	Public Function ParseDataUsingFor(source As String) As String
		Dim result As String = String.Empty
		Try
			For index As Integer = 1 To source.Length - 1 Step 2
				Dim partition As Integer = Integer.Parse($"{source(index - 1)}{source(index)}")
				If (index < 5) Then
					result += $"{partition}:"
				Else
					result += $"{partition}"
				End If
			Next
		Catch ex As Exception
			result = String.Empty
		End Try
		Return result
	End Function

	<Extension>
	Public Function ParseDataUsingLinq(source As String)
		Dim result As String = String.Empty
		Try
			result = String.Join(":", (From i In Enumerable.Range(0, 3)
									   Select Integer.Parse(source.Substring(i * 2, 2))))
		Catch ex As Exception
			result = String.Empty
		End Try
		Return result
	End Function

	<Extension>
	Public Function ParseDataUsingRegEx(source As String) As String
		Dim result As String = String.Empty
		Dim pattern = "^([0-9]{2})([0-9]{2})([0-9]{2})$"
		Dim match As Match = Nothing
		Try
			match = Regex.Match(source, pattern)
			If match.Success Then
				result = String.Join(":", (From i In Enumerable.Range(1, 3)
										   Select Integer.Parse(match.Groups(i).Value)))
			End If
		Catch ex As Exception
			result = String.Empty
		End Try
		Return result
	End Function
End Module

Open in new window

Which produces the following output -Capture.PNG-saige-
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andy GreenAuthor Commented:
Thank you. I just pulled the LINQ code and it worked just as I need.

Andy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.