For this sample code I used a Regular Expression.
The same thing can be done, converting the number to a decimal
type and using a ToString("### ### ### #####")
call.
I used an extension method so you can call it this way: "12312312312345".ToSiretNumber()
and the result will be "123 123 123 12345"
.
public static class StringExtensions
{
public static string ToSiretNumber(this string helper)
{
string siret;
try
{
siret = Regex.Replace(helper, "(?<first>([0-9]{3}))(?<second>([0-9]{3}))(?<third>([0-9]{3}))(?<fourth>([0-9]{5}))",
"${first} ${second} ${third} ${fourth}");
}
catch (ArgumentException)
{
siret = helper;
}
return siret;
}
}
If the string is not a valid SIRET number the same string is returned.
No new comments are allowed on this post.
Discussion
No comments yet. Be the first!