无法操纵字符串

时间:2010-10-23 10:00:46

标签: vb.net httprequest shoutcast

在我的网站上,我有一个脚本,它返回当前从我的shoutcast服务器播放的歌曲,格式为:“artist-track”,我想要做的是取出该字符串和将它拆分为 2个字符串:艺术家和曲目,为了做到这一点,我将服务器标签中的脚本包装成如下:

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

在我背后的代码上做了类似这样的事情:

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

问题是由于某种原因,字符串数组总是空的,实际上我根本无法进行任何操作(remove,substring.lastIndexOf等) 它总是似乎是空的。

但如果我不对字符串进行任何操作,一切都还可以,我可以看到这样的字符串:

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

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

 End Sub

任何想法为什么?

3 个答案:

答案 0 :(得分:1)

使用此...

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("-")

这会将js文件作为字符串加载,然后删除document.write语法,只留下artist-tile。

答案 1 :(得分:0)

尝试:

Dim query() As String = nowPlaying.InnerText.Split(New [Char]() {"-"c})

答案 2 :(得分:0)

刺伤但尝试:

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