How do I hide a row?
I have a string with the value "1131200001103".
How can I display it as a string in this format "11-312-001103" using Response.Write (value)?
thank
+3
mko
source
to share
4 answers
This gives the desired result
string result = Int64.Parse(s.Remove(5,2)).ToString("00-000-000000");
assuming you want to dump the 2 characters at the position of the first two zeros.
+11
Olivier jacot-descombes
source
to share
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 guessing it's overlooked that you missed two of the 0s? Not clear which 0s are, admittedly ...)
Obviously, you have to check that the string is of the correct length first ...
+12
Jon Skeet
source
to share
You can try regex and put it in ToMaskedString () extension method
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());
+2
skyfoot
source
to share
Maybe something like
string result = str.SubString(0, 2) + "-" + str.SubString(2, 3) + "-" + str.SubString(7);
str is the string "11312000011103"
+2
CodeSlinger512
source
to share