【问题标题】:How to extract a string between two delimiters in selenium by using Java?如何使用 Java 提取 selenium 中两个分隔符之间的字符串?
【发布时间】:2015-11-21 10:35:57
【问题描述】:

您好,我想从句子中提取一个字符串:DocumentId=38416184&Viewer=D&ContentFormat=pdf

这里我试图只提取 "="

之后的字符串

预期的输出是:

38416184
D
pdf

我尝试使用 split() 方法

String[] tokens = sCurrentLine.split("=|&",-1);

这里我得到一个输出:

38416184
Viewer
D
ContentFormat
pdf 

请做必要的事。提前谢谢:)

【问题讨论】:

标签: java selenium


【解决方案1】:

一切都很简单:

  1. 用“&”分割成不同的部分

  2. 在 "="

  3. 之后获取第二部分
 String[] words= str.split("&");

 foreach (var word in words)
 {
     word = word.split("=")[1]
 }

这之后的单词数组必须包含所有需要的值。

【讨论】:

    【解决方案2】:

    已经有一个 Apache 库可以做到这一点

    import java.net.URI;
    import java.net.URISyntaxException;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.utils.URLEncodedUtils;
    ...
    String url = "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java%20extract%20url%20parameters";
    List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
    for (NameValuePair param : params)
    {
        System.out.println(param.getName() + " : " + param.getValue());
    }
    

    【讨论】:

      【解决方案3】:

      到目前为止,其他答案使用split() 来表示&amp;=。如果数据不好,使用split("=") 很容易出错。请改用indexOf('=')

      String sCurrentLine = "DocumentId=38416184&Viewer=D&ContentFormat=pdf&NoValue&Complex=a=b";
      for (String param : sCurrentLine.split("&")) {
          int idx = param.indexOf('=');
          System.out.println(idx == -1 ? null : param.substring(idx + 1));
      }
      

      加上上面的坏数据,输出为:

      38416184
      D
      pdf
      null
      a=b
      

      【讨论】:

        【解决方案4】:
        String[] tokens = sCurrentLine.split("&");
        for(String token : tokens){
           System.println(token.split("=")[1]);
        }
        

        【讨论】:

          【解决方案5】:
          String[] params = sCurrentLine.split("&");
          
          List<String> paramValues = new ArrayList<String>();
          for (String p : params) {
              String pVal = p.split("=")[1];
              paramValues.add(pVal);
          }
          

          // paramValue 包含所需的输出

          【讨论】:

            【解决方案6】:

            分两次做。首先将字符串按&amp; 字符拆分以获得键值对,然后遍历这些对并按= 字符拆分。第二个令牌始终是您想要获得的值。

                String sCurrentLine = "DocumentId=38416184&Viewer=D&ContentFormat=pdf";
                for(String pair : sCurrentLine.split("&"))
                {
                    String value = pair.split("=")[1];
                    System.out.println(value);
                }
            

            【讨论】:

              【解决方案7】:

              首先你可以用“&”分割它——这样你会得到一个字符串数组,然后你可以迭代它并再次用“=”分割每个字符串。

              String str="DocumentId=38416184&Viewer=D&ContentFormat=pdf".
              String[] first=str.split("&");
              for(int i=0;i<first.length;i++) {
                 String[] then=first[i].split("=");
                 String toGet=then[1];
                 System.out.println(toGet);
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-02-10
                • 2012-11-27
                • 2017-03-17
                • 1970-01-01
                • 1970-01-01
                • 2019-04-05
                • 2020-04-19
                • 1970-01-01
                相关资源
                最近更新 更多