首页 > 解决方案 > GWT - 提取两个字符之间的文本

问题描述

在 GWT 中,我有一个 servlet,它将图像从数据库返回到客户端。我需要提取部分字符串以正确显示图像。chrome、firefox 和 IE 中返回的内容在 src 部分有一个斜线。例如:String s = "src=\""; 这在下面的字符串中不可见。也许斜线在 http 字符串周围添加了更多括号。我不确定?

  what is returned in those 3 browsers is = <img style="-webkit-user-select: none;" src="http://localhost:8080/dashboardmanager/downloadfile?entityId=4886">

EDGE 浏览器在 src 中没有斜杠,所以我提取图像的方法在边缘不起作用

什么边缘返回:

String edge = "<img src=”http://localhost:8080/dashboardmanager/downloadfile?entityId=4886”&gt;";

问题:我需要提取下面的字符串。

http://localhost:8080/dashboardmanager/downloadfile?entityId=4886

使用 src= 或 src=\

我尝试并使用不带括号“src = \”返回的浏览器:

String s = "src=\"";
int index = returned.indexOf(s) + s.length();
image.setUrl(returned.substring(index, returned.indexOf("\"", index + 1)));

但无法在 EDGE 中工作,因为它不返回斜杠

我无权访问 GWT 中的 Pattern 和 matcher。

我如何提取并记住 entityId 编号将更改 http://localhost:8080/dashboardmanager/downloadfile?entityId=4886

上面返回的字符串是什么?

编辑:

我需要一种通用的方法来提取http://localhost:8080/dashboardmanager/downloadfile?entityId=4886

当字符串可能看起来像这两种方式时。

String edge = "<img src=”http://localhost:8080/dashboardmanager/downloadfile?entityId=4886”&gt;";

3 browsers is = <img style="-webkit-user-select: none;" src="http://localhost:8080/dashboardmanager/downloadfile?entityId=4886">

标签: javaregexgwtsubstring

解决方案


    public static void main(String[] args) {
        String toParse = "<img style=\"-webkit-user-select: none;\" src=\"http://localhost:8080/dashboardmanager/downloadfile?entityId=4886\">";    
        String delimiter = "src=\"";
        int index = toParse.indexOf(delimiter) + delimiter.length();
        System.out.println(toParse.substring(index, toParse.length()).split("\"")[0]);      
    }

推荐阅读