问题
i am getting first that URL contain ad then i want to replace ad with as how to replace ad with 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.toString().contains("ad")){
data.toString() = data.toString().replace("ad", "xyz");
}
try {
webView.loadUrl(data.toString());
}
catch (Exception e){
e.printStackTrace();
}
}
回答1:
The method that may help you is below which you should put into the @override method
String myUrl = url;
myUrl = myUrl.replace("ad", as);
Then use the new string wherever you want . You can't change the original url :)
The Method replace returns the string value as API DOC says
public String replace (CharSequence target, CharSequence replacement)
Try to do this :
@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
Uri data = intent.getData();
String mString;
//want to check contains in data and if contains i want to replace it
if(data.toString().contains("ad")){
mString = data.toString().replace("ad", "xyz");
}
try {
webView.loadUrl(mString);
}
catch (Exception e){
e.printStackTrace();
}
}
来源:https://stackoverflow.com/questions/61409306/how-to-replace-url-in-webview-android-studio