Copied to clipboard

Flag this post as spam?

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


  • Hugh 30 posts 64 karma points
    Jan 26, 2014 @ 05:19
    Hugh
    0

    String Manipulation in a Partial View Macro

    Hi 

    I am trying to replace all of the spaces in a string to a hyphan.  EG:

    Fortitude Valley will equal Fortitude-Valley.  

    var sub = p.GetProperty("suburb").Value.Replace(" ", "-"); is causing me problems.  I have tried: var sub = p.GetProperty("suburb").Value.ToString.Replace(" ", "-"); al setting var sub = p.GetProperty("suburb").Value; then doing @var.Replace(" ","-") but not go. 

    Any help here please?

    Thanks

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 26, 2014 @ 05:43
    Fuji Kusaka
    0

    How about

    var sub = p.GetProperty("suburb").Value.ToString().Replace(" ", "-")
  • Hugh 30 posts 64 karma points
    Jan 26, 2014 @ 05:46
    Hugh
    0
    varsub= p.GetProperty("suburb").Value.ToString().Replace(" ","-");
    works thanks :)
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 26, 2014 @ 09:29
    Jeavon Leopold
    0

    Generally it's better to use GetPropertyValue than GetProperty, e.g

    var sub= p.GetPropertyValue("suburb").ToString().Replace(" ","-");
    

    One further improvement, you can ensure you are returned a string by specifying the return type, e.g

    var sub= p.GetPropertyValue<string>("suburb").Replace(" ","-");
    

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft