Android indexoutofboundsexception
本文关键字:indexoutofboundsexception Android | 更新日期: 2023-09-27 18:01:37
大家好,我在用正则表达式。我有indexoutofboundexception这是我的来源
public String ExtractYoutubeURLFromHTML(String html) throws UnsupportedEncodingException
{
Pattern pattern = Pattern.compile(this.extractionExpression);
Matcher matcher = pattern.matcher(html);
List<String> matches = new ArrayList<String>();
while(matcher.find()){
matches.add(matcher.group());
}
String vid0 = matches.get(0).toString();
vid0 = vid0.replace ("''''u0026", "&");
vid0 = vid0.replace ("''''''", "");
vid0=URLDecoder.decode(vid0,"UTF-8");
return vid0;
}
我调试,我有indexoutofboundexception无效索引异常
错误在这部分代码
List<String> matches = new ArrayList<String>();
while(matcher.find()){
matches.add(matcher.group());
}
String vid0 = matches.get(0).toString();
我也有c#工作代码,我想在java代码中重写c#代码这是c#中的工作代码
public string ExtractYoutubeURLFromHTML(string html)
{
Regex rx = new Regex (this.extractionExpression);
var video = rx.Matches (html);
var vid0 = video [0].ToString ();
vid0 = vid0.Replace ("''''u0026", "&");
vid0 = vid0.Replace ("''''''", "");
vid0 = System.Net.WebUtility.UrlDecode (vid0);
return vid0;
}
如果matches
为空,则matches.get(0).toString();
将给出ArrayIndexOutOfBoundsException
。
最好检查一下
if(matches.size()>0)
String vid0 = matches.get(0).toString();
替换这个
String vid0 = matches.get(0).toString();
if(matches.size() > 0){
String vid0 = matches.get(0).toString();
} else {
return "" // or you can also return null;
}