【问题标题】:GWT - extract text in between two charactersGWT - 提取两个字符之间的文本
【发布时间】:2019-07-02 23:47:25
【问题描述】:

在 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”>";

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

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”>";

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

【问题讨论】:

    标签: java regex gwt substring


    【解决方案1】:
        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]);      
        }
    

    【讨论】:

    • 在其 String toParse = "localhost:8080/dashboardmanager/downloadfile?entityId=4886">";我需要一种方法来提取 http 部分。不管字符串如何开始。
    • 如果您更改以下的 toParse 声明,提供的示例仍然有效: String toParse = "localhost:8080/dashboardmanager/downloadfile?entityId=4886\">";
    • 不,您在上面发布的那个有斜线,但不应该也不会有斜线。第一个有斜线,第二个没有。示例 String toParse = "";
    • 您的示例可以使用斜线还是不使用斜线?
    • 斜线只是用来转义字符串声明中的双引号。所以是的,它会的。
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 2016-08-13
    • 2018-10-04
    • 2013-05-14
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    相关资源
    最近更新 更多