views:

35

answers:

3

In my website i have a script that return the current song that playing from my shoutcast server in a format of: "artist-track", what i want to do is to take that string and split it to 2 strings: artist and track, in order to do that i wrapped the script in server tag like this:

<div id="nowPlaying" runat="server">
<script type="text/javascript" src="http://shoutcast.mixstream.net/js/song/uk3-free:34588"&gt;&lt;/script&gt;     
</div>

and on code behind i did somthing like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim query() As String = nowPlaying.InnerText.Split("-")
        Dim artist As String = query(0)
        Dim track As String = query(1)
        Response.Write(artist + "<br />" + track)

    End Sub

the problem is that for some reason the string array is allways empty in fact i am unable to do any manipulation at all (remove, substring. lastIndexOf etc.) it allways seems to be empty.

but if i dont do any manipulation on the string everything is ok and i can see the string like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim query As String = nowPlaying.InnerText
        Response.Write(query)

 End Sub

any ideas why?

A: 

Try:

Dim query() As String = nowPlaying.InnerText.Split(New [Char]() {"-"c})
Jakub Konecki
tried it but still not spliting...
tone
try: Dim query() As String = "This is-a test".Split(New [Char]() {"-"c})
Jakub Konecki
with "This is-a test" its working, with nowPlaying.InnerText.Split still not working.
tone
It means that your nowPlaying.InnerText is not a string with a dash inside...
Jakub Konecki
if i navigate to the script url this is what i see: document.write('artist - track'); - because i am not a javascript expert i can only take a guess that this is the problem...any ideas?
tone
A: 

Bit of a stab but try:

Dim sTrim as String = nowPlaying.InnerText
sTrim=sTrim.Replace("document.write('","")
sTrim=sTrim.SubString(0,sTrim.length-3)
Dim query() As String = sTrim.Split("-")
El Ronnoco
not working...iv'e updated the page in the server with your code if you would like to see:http://www.radioz.co.il/iframe/iframe2.aspx
tone
Can you insert a breakpoint on the second line of my code just after sTrim has been initialised and see what it is set to?
El Ronnoco
<script type="text/javascript" src="http://shoutcast.mixstream.net/js/song/uk3-free:34588"></script>, well i guess now we know why its not working because its not an actual string. my fault i should check it before i even posted the question.
tone
:) Do you actually need the artist and track in the code-behind? Or is it just so you can show it on the page? If you just want it to appear on the page then I'd recommend doing the Split in Javascript after the page has rendered - that way you will be able to get the actual Artist - Title string that you need.
El Ronnoco
in need it because i am using it to query last.fm track.search that need this 2 parameters separately
tone
+1  A: 

Use this...

Dim request As HttpWebRequest = WebRequest.Create("http://shoutcast.mixstream.net/js/song/uk3-free:34588")
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim sTrim = reader.ReadToEnd()

reader.Close()
sTrim=sTrim.Replace("document.write('","")
sTrim=sTrim.SubString(0,sTrim.length-3)
Dim query() As String = sTrim.Split("-")

This loads the js file as a string and then removes the document.write syntax to leave you just artist-tile.

El Ronnoco
working like a charm! thank you very much for your help.
tone
Be careful as characters such as apostrophe (`'`) may come out in HTML safe format ie `'` etc etc. Just be aware to give it a good test. I would also have thought that there should be a more robust way of retrieving this information? But if it works it works! :) Thanks for the tick.
El Ronnoco