Patrick Patrick - 1 year ago 85
C# Question

Replace end of string with dots if word is longer then X

How can I format a string in a Razor CSHTML page if it's longer then X caracters:


Example for an X = 9

-> if Council is "Lisbon", then the result is "<p>Lisbon</p>"
-> if Council is "Vila Real de Santo António", then the result is "<p>Vila Real...</p>" with the title over the <p> "Vila Real de Santo António" showing the complete information


Answer Source

for any string. See here.

And for your code...

@(Model.Council.Length>10 ? Model.Council.Substring(0, 10)+"..." : Model.Council)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download