Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Formatting Strings
Message
 
 
To
08/10/2004 05:27:51
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00949820
Message ID:
00950016
Views:
22
>Good afternoon to all! Is there an easier way to format a string? Like my account code is a string and should be formated 999-999-99. I can't make use of the mid function as the code will be too long especially since the value of the account code can vary in length (between 1-8 characters).
>
>I tried making use of the Format command which works well only if the string contains 8 characters. If it containst less, it is still formatted but in the wrong way:
>
>x="12345"
>x=format(val(x),"000-000-00")
>Result: "000-123-45"
>
>Ideally it should look like: "123-45 - "
' Here is the basic algorithm you need, when you are done testing simply change "xxx-xxx-xx" into "   -   -  "
Dim AccountCode As String = "12345"
Dim triplets As Int16 = CShort(Math.Floor((AccountCode.Length - 1) / 3))
Dim AccountCodeFormatted As String = Format(Val(AccountCode), "###-###-##".Substring(0, AccountCode.Length + triplets)) _
				+ "xxx-xxx-xx".Substring(AccountCode.Length + triplets)

' And here is some test code that will write to the Output window.
System.Diagnostics.Debug.WriteLine(smartFormat("1"))
System.Diagnostics.Debug.WriteLine(smartFormat("12"))
System.Diagnostics.Debug.WriteLine(smartFormat("123"))
System.Diagnostics.Debug.WriteLine(smartFormat("1234"))
System.Diagnostics.Debug.WriteLine(smartFormat("12345"))
System.Diagnostics.Debug.WriteLine(smartFormat("123456"))
System.Diagnostics.Debug.WriteLine(smartFormat("1234567"))
System.Diagnostics.Debug.WriteLine(smartFormat("12345678"))

Public Function smartFormat(ByVal AccountCode As String) As String
	Dim lengthPlusHyphens As Int32 = AccountCode.Length _
				+ CInt(Math.Floor((AccountCode.Length - 1) / 3))
	Return Format(Val(AccountCode), "###-###-##".Substring(0, lengthPlusHyphens)) _
			+ "xxx-xxx-xx".Substring(lengthPlusHyphens)
End Function
censored.
Previous
Reply
Map
View

Click here to load this message in the networking platform