11

I have a string with value "1131200001103".

How can I display it as a string in this format "11-312-001103" using Response.Write(value)?

Thanks

mko
  • 6,638
  • 12
  • 67
  • 118
  • Possible Dup - http://stackoverflow.com/questions/5509172/apply-mask-to-a-string – Micah Armantrout Mar 14 '12 at 16:30
  • and you are looking to accomplish what? because that thing can be done using substrings and concatenate – gbianchi Mar 14 '12 at 16:32
  • Please read more carefully, this is not a dup. – mko Mar 14 '12 at 16:32
  • @gbianchi that would be my alternative. I am looking for a better solution/approach – mko Mar 14 '12 at 16:33
  • 1
    So, to make your question better, explain why substring is not a solution, or why you think there should be a better solution. substring is too slow? too much code? too hard on some cases?? string can be variable lenght? Look at Jon answer... – gbianchi Mar 14 '12 at 16:39
  • Are you looking for a C# version of C++'s `scanf` function? – Brian Mar 14 '12 at 17:53
  • @John: No, I was just trying to understand your question. – Brian Mar 15 '12 at 13:04

7 Answers7

18

This produces the required result

string result = Int64.Parse(s.Remove(5,2)).ToString("00-000-000000");

assuming that you want to drop 2 characters at the position of the 2 first nulls.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
16

Any reason you don't want to just use Substring?

string dashed = text.Substring(0, 2) + "-" +
                text.Substring(2, 3) + "-" +
                text.Substring(7);

Or:

string dashed = string.Format("{0}-{1}-{2}", text.Substring(0, 2),
                              text.Substring(2, 3), text.Substring(7));

(I'm assuming it's deliberate that you've missed out two of the 0s? It's not clear which 0s, admittedly...)

Obviously you should validate that the string is the right length first...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • And OP looks for a better solution according to his comment.. is there a better solution?? – gbianchi Mar 14 '12 at 16:37
  • I appreciate Jon's answer, that is almost identical to my solution. Oliver's solution is also a good one. I just wanted to see is there a masking solution similar to text-box masking. – mko Mar 14 '12 at 17:13
  • @Jon Two 0s are deliberate. I am actually masking third party response codes, but I guess 8 digits were too much for their documentation as well. They are using 1131200001103 as a raw response and 11-312-001103 in their documentation. – mko Mar 14 '12 at 17:16
  • @John: Do you know which of the 0s should be removed though? For example, if the original was 11111145671111 would the result be 11-111-451111 or 11-111-671111 - or something different? – Jon Skeet Mar 14 '12 at 17:26
  • First two 0s of the third part. So the result would be 11-111-671111. ##-###-######. You have extra chars in your original string. – mko Mar 15 '12 at 10:00
11

You can try a regular expression and put this inside an extension method ToMaskedString()

public static class StringExtensions
{
    public static string ToMaskedString(this String value)
    {
        var pattern = "^(/d{2})(/d{3})(/d*)$";
        var regExp = new Regex(pattern);
        return regExp.Replace(value, "$1-$2-$3");
    }
}

Then call

respne.Write(value.ToMaskedString());
skyfoot
  • 20,629
  • 8
  • 49
  • 71
5

I wrote a quick extension method for same / similar purpose (similar in a sense that there's no way to skip characters).

Usage:

var testString = "12345";
var maskedString = testString.Mask("##.## #"); // 12.34 5

Method:

public static string Mask(this string value, string mask, char substituteChar = '#')
{
    int valueIndex = 0;
    try
    {
        return new string(mask.Select(maskChar => maskChar == substituteChar ? value[valueIndex++] : maskChar).ToArray());
    }
    catch (IndexOutOfRangeException e)
    {
        throw new Exception("Value too short to substitute all substitute characters in the mask", e);
    }
}
ESipalis
  • 381
  • 4
  • 17
3

Maybe something like

string result = str.SubString(0, 2) + "-" + str.SubString(2, 3) + "-" + str.SubString(7);

str being the "11312000011103" string

CodeSlinger512
  • 618
  • 1
  • 8
  • 23
1

Here's a C# 8 approach you can take:

Response.Write($"{value[..2]}-{value[2..5]}-{value[^6..]}");
Michael
  • 11,571
  • 4
  • 63
  • 61
0

I've complemented the Answer from @ESipalis

The extension method accepts a Array of chars to be replaced

public static string Mask(this string value, string mask, char[] substituteChar)
  {
     int valueIndex = 0;
     try
     {
        return new string(mask.Select(maskChar => substituteChar.Contains(maskChar) ? value[valueIndex++] : maskChar).ToArray());
     }
     catch (IndexOutOfRangeException e)
     {
        return "#ERROR";
     }
  }

And you can call the extension:

yourString.Mask("AAA-999(SSS)","AS9".ToArray())
Daniel
  • 2,780
  • 23
  • 21