1

Every time an artist with an "&" symbol in their name comes up I get on my labels "R3HAB VINAI" instead of "R3HAB & VINAI" I would like to get the whole string as it is more useful to the end user. What is weird is the "&" symbol shows up in the track name just fine. Maybe someone can tell me what I'm doing wrong.

last.fm JSON API data from user.getRecentTracks

{  
recenttracks:{  
    track:[  
        {  
            artist:{  
                #text:"R3HAB & VINAI",
                mbid:"eb0fbe2c-6eb7-40bc-900b-3e056c158125"
            },
            name:"HOW WE PARTY",
            streamable:"0",
            mbid:"b410da12-f308-4e2f-af88-19c69312e450",
            album:{  
                #text:"",
                mbid:""
            },
            url:"https://www.last.fm/music/R3HAB+&+VINAI/_/HOW+WE+PARTY",
            image:[  
                {  
                    #text:"",
                    size:"small"
                },
                {  
                    #text:"",
                    size:"medium"
                },
                {  
                    #text:"",
                    size:"large"
                },
                {  
                    #text:"",
                    size:"extralarge"
                }
            ],
            @attr:{  
                nowplaying:"true"
            }
        },
        {  
            artist:{  
                #text:"Alan Walker",
                mbid:""
            },
            name:"Sing me to Sleep",
            streamable:"0",
            mbid:"",
            album:{  
                #text:"",
                mbid:""
            },
            url:"https://www.last.fm/music/Alan+Walker/_/Sing+me+to+Sleep",
            image:[  
                {  
                    #text:"",
                    size:"small"
                },
                {  
                    #text:"",
                    size:"medium"
                },
                {  
                    #text:"",
                    size:"large"
                },
                {  
                    #text:"",
                    size:"extralarge"
                }
            ],
            date:{  
                uts:"1501613749",
                #text:"01 Aug 2017, 18:55"
            }
        }
    ],
    @attr:{  
        user:"SomeName",
        page:"1",
        perPage:"1",
        totalPages:"8676",
        total:"8676"
    }
}
}

C# class listing

    public class Rootobject
    {
        public Recenttracks recenttracks { get; set; }
    }

    public class Recenttracks
    {
        public Track[] track { get; set; }
        public Attr attr { get; set; }
    }

    public class Attr
    {
        public string user { get; set; }
        public string page { get; set; }
        public string perPage { get; set; }
        public string totalPages { get; set; }
        public string total { get; set; }
    }

    public class Track
    {
        public Artist artist { get; set; }
        public string name { get; set; }
        public string streamable { get; set; }
        public string mbid { get; set; }
        public Album album { get; set; }
        public string url { get; set; }
        public Image[] image { get; set; }
        public Attr1 attr { get; set; }
        public Date date { get; set; }
    }

    public class Artist
    {
        [JsonProperty("#text")]
        public string text { get; set; }
        public string mbid { get; set; }
    }

    public class Album
    {
        public string text { get; set; }
        public string mbid { get; set; }
    }

    public class Attr1
    {
        public string nowplaying { get; set; }
    }

    public class Date
    {
        public string uts { get; set; }
        public string text { get; set; }
    }

    public class Image
    {
        public string text { get; set; }
        public string size { get; set; }
    }
Manvaril
  • 160
  • 3
  • 15
  • Where are you displaying these labels? On a web page? Winforms? Both need escaping, but in different ways – Brian Rogers Aug 01 '17 at 19:16
  • Like on a WinForm Label? I believe you need to double up the `&`. So do a replace of `&` with `&&`. https://stackoverflow.com/questions/4325094/enter-symbol-into-a-text-label-in-windows-forms – TyCobb Aug 01 '17 at 19:16
  • depends on how you're displaying the string. We need to know if it's a winform, xml, html, etc. the `&` is interpreted differently depending on your case. – Capn Jack Aug 01 '17 at 19:19
  • In a winforms program it will also be saved to a text file – Manvaril Aug 01 '17 at 19:20

3 Answers3

4

You most likely need to escape the ampersand.

  • If you're using windows forms and displaying it in a label, you can technically do it without escaping the ampersand, by switching the UseMnemonic property to false on the label.
  • If you're using anything other than a label it can be done with a TextFormatFlag if you don't want to escape it
  • You can escape just that character, by using &&
  • If you're displaying it on the web, you'll need to escape it using HttpUtility.HtmlEncode() or by switching all & to &
  • If you prefer another way to encode it, you can use WebUtility.HtmlEncode() which does not have a dependency on System.Web
Brian Deragon
  • 2,929
  • 24
  • 44
2

By default, the ampersand (&) is interpreted as "underline the following character and make it a keyboard shortcut." (and of course do not display the ampersand.) So, you have two options: either

  • set the UseMnemonic property of the label to false, telling it to not try to interpret any ampersands, or

  • replace each ampersand with two ampersands, which are a special sequence which means "leave this ampersand alone". (and of course display only one ampersand.)

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

When displaying to a WinForm you need to escape the ampersand to display it. You can do this by replacing all occurrences of it with && instead.

Before setting your artist, perform this on the string containing it:

artist = artist.Replace("&", "&&");

Capn Jack
  • 1,201
  • 11
  • 28