Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • suzyb 476 posts 934 karma points
    Mar 11, 2014 @ 11:14
    suzyb
    0

    Convert and remove line breaks partial view

    I'm trying to format some html that will be contained within a string for use in javascript. One of the fields I am including is a text area that can have line breaks in it.

    To replace these with br tags I am using umbraco.library.ReplaceLineBreaks which is adding the br tags. However there are also still line breaks in the text that is causing an error in the javascript.

    This is the output I am getting

    html:     '<strong>Leeds Office</strong><br />4 Church Walk <br/>
    Leeds<br/>
    LS2 7EG<br /><a href="http://maps.google.co.uk/maps?daddr=53.79575610280947,-1.5367474779097847" target="_blank">Get Directions</a>'
    

    but this is what I need.

    html:     '<strong>Leeds Office</strong><br />4 Church Walk <br/>Leeds<br/>LS2 7EG<br /><a href="http://maps.google.co.uk/maps?daddr=53.79575610280947,-1.5367474779097847" target="_blank">Get Directions</a>'
    

    Simply replacing Environment.NewLine doesn't seem to do anything so how can I remove the line breaks from the string.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 11, 2014 @ 11:48
    Dennis Aaen
    0

    Hi suzyb,

    Maybe you could try use the ReplaceLineBreaks library method.

    http://our.umbraco.org/forum/developers/razor/39891-ReplaceLineBreaks-in-Razor?p=0#comment145788

    Hope this helps you.

    /Dennis

  • Dan Lister 416 posts 1974 karma points c-trib
    Mar 11, 2014 @ 12:11
    Dan Lister
    101

    After running the ReplaceLineBreaks method you could use the Replace method to remove any line breaks.

    var cleanedString = myString.Replace("\r", "").Replace("\n", "").Trim();
  • suzyb 476 posts 934 karma points
    Mar 11, 2014 @ 13:31
    suzyb
    1

    Thanks Dan.

    I was trying myString.Replace(Environment.Newline, "") which didn't seem to work but your way does.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies