假设我有一个包含HTML文件的字符串。如何仅将脚本部分作为子字符串获取?
示例字符串为:
str="<html>...<script>...</script>...</html>"
我只需要脚本部分。
<script type = "text/javascript">
function Showques()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//getting response from servlet
xmlhttp.onreadystatechange=function();
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var localString=xmlhttp.responseText;
var locationstart =localString.indexOf('<script>');
var locationend = localString.indexOf('</script>');
//eval(xmlhttp.responseText);
document.getElementById("ques").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","XmlApp",true);
xmlhttp.send();
}
</script>
当我在
</script>
方法中提供indexOf
时,它被认为是我脚本的结尾,我在开头和结尾声明了错误。 最佳答案
String str = "yourStringTosearch"
int location = str.indexOf('String');
string.substring(location , to)
可以根据您的示例执行此操作
String str = "<html>...<script>...</script>...</html>"
int locationStart = str.indexOf('<script>');
int locationend = str.indexOf('</script>');
strinf finalString =string.substring(locationStart , locationend ); //ur final string value