简体   繁体   中英

Formatting String in Razor with String.Format not working correctly

I'm having an issue formatting a simple string in Razor, I've verified that the syntax is correct in regular C# code, but once I migrate it to my HTML page using Razor and String.Format doesn't seem to work. I'm trying to format a phone number string from ########## to (###)###-#### so my code for this is @{ string number = contact.ContactNumber; string formattedNumber = String.Format("({0}){1}-{2}", number.Substring(0, 3), number.Substring(3, 3), number.Substring(6, 4)); } @{ string number = contact.ContactNumber; string formattedNumber = String.Format("({0}){1}-{2}", number.Substring(0, 3), number.Substring(3, 3), number.Substring(6, 4)); }

I'm trying to verify if this is correct just by doing alert(@formattedNumber) but it doesn't appear to be working, the alert won't even appear. However if my code is simply

@{ string number = contact.ContactNumber; string formattedNumber = String.Format("{0}{1}{2}", number.Substring(0, 3), number.Substring(3, 3), number.Substring(6, 4)); }

Note that this one should just display the regular number, and the alert will work correctly and display ########## Any suggestions on why the (###)###-#### isn't working?

Your formatting is correct, it's just the way you output it on the alert .

When you use @formattedNumber in your View this is substituted for the value in that variable. Passing that string directly to a JavaScript alert will not work in the first instance because you won't end up with a valid string to alert . It will work in the second instance as you will be left with a valid number passed to the alert function.

Imagining an example where contact.ContactNumber is 0123456789 the first example will give JavaScript code of

alert((012)345-6789);

which is invalid.

The second example will work because it gives JavaScript code of

alert(0123456789);

which is valid (it will alert 123456789 in this example).

As HaukurHaf points out in the comments, you actually want to output the result as a string so you need single quotes around the output in your JavaScript:

alert('@formattedNumber'); //note the single quotes

This will mean in the first example you'll get an output of:

alert('(012)345-6789');

Check your console, my guess is you're getting a JS error causing the alert to fail. Here's your code with output along with an alternative way to do this with regexes.

var number = "1112223333";

//an alternative method using regexes
var regexFormat = Regex.Replace(number, @"^(\d{3})(\d{3})(\d{4})$", "($1)$2-$3");

//The methods you provided
string formattedNumber = String.Format("{0}{1}{2}", number.Substring(0, 3), number.Substring(3, 3), number.Substring(6, 4));
string formattedNumber1 = String.Format("({0}){1}-{2}", number.Substring(0, 3), number.Substring(3, 3), number.Substring(6, 4));

Here's the output from this:

     regexFormat = (111)222-3333
 formattedNumber = 1112223333
formattedNumber1 = (111)222-3333

regexFormat and formattedNumber1 should both provide you with what you need. I personally find the regex easier to read in this instance.

How are you printing out the number in your HTML? Doing the following works just fine for me:

    @{
        string number = "0712345678";
        string formattedNumber = String.Format("({0}){1}-{2}", number.Substring(0, 3), number.Substring(3, 3), number.Substring(6, 4));
    }

    <div>@formattedNumber</div>

I suspect your problem isn't with the formatting of the string (the way you have it is perfectly legit) but possibly with the way you're trying to display the string?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM