【问题标题】:obtaining each instance of a substing between two other substrings获取两个其他子字符串之间的子字符串的每个实例
【发布时间】:2014-12-11 06:03:36
【问题描述】:

对于我正在编写的程序,我在字符串中存储了来自 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());
        }
    }        
}

【问题讨论】:

    标签: java string search substring


    【解决方案1】:

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

    重复查找您想要的下一个子字符串的开始。

    获取中间的文本。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2013-09-12
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多