Format string with dashes
Asked Answered
D

6

16

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?

Drank answered 19/10, 2010 at 13:28 Comment(0)
C
39
Regex.Replace("410151000640", @"^(.{2})(.{2})(.{2})(.{3})(.{3})$", "$1-$2-$3-$4-$5");

Or the slightly shorter version

Regex.Replace("410151000640", @"^(..)(..)(..)(...)(...)$", "$1-$2-$3-$4-$5");
Chessman answered 19/10, 2010 at 13:36 Comment(4)
Works like a charm! I extracted this into a wrapper method with some validation and error handling. Thanks!Drank
I must really learn Regex properly myself. A very good solution to this problem.Mizzle
@Øyvind If it helps, I like to use regular-expressions.info/tutorial.html for reference. They also talk about the different flavors of regex provided by different engines.Chessman
Thanks. I'll read up on RegExp there. Looks like a good source of information.Mizzle
H
11

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));
}
Houselights answered 19/10, 2010 at 13:39 Comment(1)
This works nicely. So it really comes down to whether to use regular expressions (of which I'm not an expert) or not.Malda
H
3

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, "-");
}
Hf answered 19/10, 2010 at 13:33 Comment(0)
E
3

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();
Elenore answered 19/10, 2010 at 14:4 Comment(1)
Shouldn't the 'a' in your mask be '&'? 'a' is Alphanumeric, optional. The only characters it will accept are the ASCII letters a-z and A-Z. msdn.microsoft.com/en-us/library/…Thinkable
D
1

Here is a simple extension method with some utility:

    public static string WithMask(this string s, string mask)
    {
        var slen = Math.Min(s.Length, mask.Length);
        var charArray = new char[mask.Length];
        var sPos = s.Length - 1;
        for (var i = mask.Length - 1; i >= 0 && sPos >= 0;)
            if (mask[i] == '#') charArray[i--] = s[sPos--];
            else
                charArray[i] = mask[i--];
        return new string(charArray);
    }

Use it as follows:

        var s = "276000017812008";
        var mask = "###-##-##-##-###-###";
        var dashedS = s.WithMask(mask);

You can use it with any string and any character other than # in the mask will be inserted. The mask will work from right to left. You can tweak it to go the other way if you want.

Have fun.

Dobsonfly answered 27/7, 2018 at 17:40 Comment(0)
C
0

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
Calices answered 19/10, 2010 at 13:45 Comment(1)
No, the letters also are included in the parcel number, for instance "410151000vb6" should become "41-01-51-000-vb6"Drank

© 2022 - 2024 — McMap. All rights reserved.