views:

62

answers:

5

I have a compressed string value I'm extracting from an import file. I need to format this into a parcel number, which is formatted as follows: ##-##-##-###-###. So therefore, the string "410151000640" should become "41-01-51-000-640". I can do this with the following code:

String.Format("{0:##-##-##-###-###}", Convert.ToInt64("410151000640"));

However, The string may not be all numbers; it could have a letter or two in there, and thus the conversion to the int will fail. Is there a way to do this on a string so every character, regardless of if it is a number or letter, will fit into the format correctly?

+1  A: 

This should work in your case:

string value = "410151000640";
for( int i = 2; i < value.Length; i+=3){
  value = value.Insert( i, "-");
}

Now value contains the string with dashes inserted.

EDIT

I just now saw that you didn't have dashes between every second number all the way, to this will require a small tweak (and makes it a bit more clumsy also I'm afraid)

string value = "410151000640";
for( int i = 2; i < value.Length-1; i+=3){
  if( value.Count( c => c == '-') >= 3) i++;
  value = value.Insert( i, "-");
}
Øyvind Bråthen
+5  A: 
Regex.Replace("410151000640", @"^(.{2})(.{2})(.{2})(.{3})(.{3})$", "$1-$2-$3-$4-$5");
Yuriy Faktorovich
Works like a charm! I extracted this into a wrapper method with some validation and error handling. Thanks!
Kevin
I must really learn Regex properly myself. A very good solution to this problem.
Øyvind Bråthen
@Øyvind If it helps, I like to use http://www.regular-expressions.info/tutorial.html for reference. They also talk about the different flavors of regex provided by different engines.
Yuriy Faktorovich
@Yuriy - Thanks. I'll read up on RegExp there. Looks like a good source of information.
Øyvind Bråthen
+2  A: 

I would approach this by having your own formatting method, as long as you know that the "Parcel Number" always conforms to a specific rule.

public static string FormatParcelNumber(string input)
{
  if(input.length != 12)
     throw new FormatException("Invalid parcel number. Must be 12 characters");

  return String.Format("{0}-{1}-{2}-{3}-{4}",
                input.Substring(0,2),
                input.Substring(2,2),
                input.Substring(4,2), 
                input.Substring(6,3), 
                input.Substring(9,3));
}
Jamiec
A: 

If i understodd you correctly youre looking for a function that removes all letters from a string, aren't you?

I have created this on the fly, maybe you can convert it into c# if it's what you're looking for:

Dim str As String = "410151000vb640"
str = String.Format("{0:##-##-##-###-###}", Convert.ToInt64(MakeNumber(str)))


Public Function MakeNumber(ByVal stringInt As String) As String
    Dim sb As New System.Text.StringBuilder
    For i As Int32 = 0 To stringInt.Length - 1
        If Char.IsDigit(stringInt(i)) Then
            sb.Append(stringInt(i))
        End If
    Next
    Return sb.ToString
End Function
Tim Schmelter
No, the letters also are included in the parcel number, for instance "410151000vb6" should become "41-01-51-000-vb6"
Kevin
A: 

If its part of UI you can use MaskedTextProvider in System.ComponentModel

    MaskedTextProvider prov = new MaskedTextProvider("aa-aa-aa-aaa-aaa");
    prov.Set("41x151000a40");
    string result = prov.ToDisplayString();
anivas