java - 获取两个其他子字符串之间的子字符串的每个实例

标签 java string search substring

对于我编写的程序,我将 html 文件中的信息存储在字符串中。 html 文件是导出的书签文件,我需要我的程序搜索 html 文件并将每个实例存储到特定站点的书签,以便我可以进行进一步处理。

书签有一致的开始,但没有一致的结束,例如

https://www.example.net/e/1111111/1/example
https://www.example.net/e/2222222/1/
https://www.example.net/e/3333333/1
https://www.example.net/e/4444444

在html文件中,url后面是引号,但我不确定如何使用它来获取url。

如果有人能指出我正确的方向,我会很感激

感谢 @mafagafogigante 的帮助,它使我能够生成以下代码:

public static void FileforURL(String content){
    int first, second;

   while(content.indexOf("https://www.example.net/e/") != -1){
        first = content.indexOf("https://www.example.net/e/");
        if(content.indexOf("\"",first) != -1){
            second = content.indexOf("\"",first);

            try {
                URL(content.substring(first, second));
            } catch (Exception e) {
                e.printStackTrace();
            }
            content = content.substring(second,content.length());
        }
    }        
}

最佳答案

使用 String.indexOf(...) 获取下一个“https://[...]”子字符串开头。

重复查找所需下一个子字符串的开头。

获取中间的文本。

重复直到得到 -1,剩下的就是您的最后一个 URL。

关于java - 获取两个其他子字符串之间的子字符串的每个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26394786/

相关文章:

java - java中的动画: paint() and repaint() or threads

java - 尝试使用 String.split ("\\?"时出现意外行为)

python - 在 Python 中的一组许多 "range"对象中搜索值的最快方法是什么

search - imap "search all"范围 : all folders/current folder?

java - 需要 Jbuttons 透明外观

java - 未在 Android 应用程序中使用 SQLite 创建表

java - 如何在GWT中的一组下添加复选框?

Python 切片字符串或添加到列表列表中的字符串

java - 将字符串格式化为小数点后两位

Python Django "and/or"在数据库中搜索