首页 > 解决方案 > 如何检查 Uri 数据是否包含一些单词,然后在 webview 中替换它?

问题描述

我首先检查 Uri 数据是否包含 ad,然后我想将 ad 替换为 as 如何将 ad 替换为 as。

  @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        Uri data = intent.getData();

 //want to check contains in data and if contains i want to replace it

        if(data.contains("ad")){
            data.replace("ad", "as");
        }
        try {
            webView.loadUrl(data.toString());
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

标签: androidwebview

解决方案


Uri 没有containsand replace,因此您必须先转换为String

试试这个:

if (data != null) {
    String uriString = data.toString();
    if (uriString.contains("ad")) {
        uriString = uriString.replace("ad", "as");
    }
    try {
       webView.loadUrl(uriString);
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

推荐阅读