Reputation: 41
I got here an sample string output in numbers:
123456789
But What my goal is to make it like this:
Format: 123-45-6789
I was able to look for some codes here but they all have a fix interval , which is not fit from what I am making. It is required to make the input format to be like this "123-45-6789" but when saving it is only required 9 characters long because there is a limit to the space where it should be stored so I used this code to met the 9 characters long storation.
Input.Text = Input.Text.Trim().Replace("-", string.Empty);
Bio.SetString(UserName, "9Character", Input.Text.Trim());
But when displaying it again , it is again required to be on this format , 123-45-6789. Which is my problem.
Upvotes: 0
Views: 704
Reputation: 26312
something like,
string number = "123456789";
var output = Regex.Replace(number,
@"^(\d{3})[ -]?(\d{2})[ -]?(\d{4})( x\d+)?",
@"$1-$2-$3$4", RegexOptions.IgnorePatternWhitespace);
Console.WriteLine($"formated {output}");
output : formated 123-45-6789
Upvotes: 0
Reputation: 4218
You can also try something like:
string val = "123456789";
val = val.Substring(0, 3) + "-" + val.Substring(3, 2) + "-" + val.Substring(5) ;
Here is a working DEMO
Upvotes: 1