I'm working on Java with URL and I don't know how to encode the " ' " character.
I don't want to use URLEncoder since it replaces spaces with + and I need %20
private String encoder(String param) {
return param.replaceAll("\\s", "%20").replaceAll("\\'", "%27");
}
This is the code i'm trying but it doesn't work, do you have any ideas on how to replace this " ' " ? Or know another method better than it?
Thank you!!
You don't need to escape ' symbol, just enclose it in double quotes
param.replaceAll("\\s", "%20").replaceAll("'", "%27");
Another way is to use java.net.URLEncoder class to encode URL and replace + symbols to %20
java.net.URLEncoder.encode("URL with spaces'", "UTF-8").replace("+", "%20")
Related
I have the following piece of code :
public String createDownloadLink(String user) {
String url = "No file exists";
String fileName = "download.zip";
if (userExists())
url = "<a onClick= 'downloadfile(\"" + user + "\", \"" + Encode.forJavaScript(fileName) + "\")' href='#' >Download</a>";
return url;
}
In the above piece of code, the user value is their respective email address.
This code works fine for all email addresses except for email addresses having an apostrophe in it.
Example : For user = "john.d'cruz#java.com" , the url I get in response is
<a onclick="downloadfile("john.d" cruz#java.com", "download.zip")' href="#">Download</a>
And this is a broken url so the download fails.
I tried using
user = user.replace("'","\'");
But it still gives me the same error.
How do I fix this?
If I understand correctly, you just need to escape the ' with a \, right?
If so, your method for escaping the ' sign should look like this instead.
user = user.replace("'","\\'");
Java uses \ for escaping characters, so in your method you were saying ' should be replaced with an escaped ' which basically translates back to the '. Please correct me if I understood the question incorrectly.
For escaping characters you could use:
StringEscapeUtils.escapeJava("john.d'cruz#java.com");
which results in john.d\'cruz#java.com.
Add below dependency to your project:
https://mvnrepository.com/artifact/org.apache.commons/commons-text
and this import statement:
import org.apache.commons.text.StringEscapeUtils;
Something like this should work in your case:
public String createDownloadLink(String user) {
if (userExists()) {
return String.format("<a onClick=\"downloadFile('%s', 'download.zip')\" href=\"#\" >Download</a>", StringEscapeUtils.escapeJava(user));
}
return "No file exists";
}
I have a string output which I need to match and I am using a regex
String schemaName = "Amazon";
String test = "{\"data\": [], \"name\": \"Amazon\", \"title\": \"StoreDataConfig\"}";
String output= method("\\[\\]",schemaName);
Matcher n = Pattern.compile(output).matcher(test);
boolean available = n.find();
System.out.println(available);
I wanted to validate the same and passing the regex to a method as mentioned
private static String method(String data, String schemaName) throws IOException {
System.out.println(data);
return ("{\"data\": " + data + ", \"name\": " + "\"" + schemaName + "\"" + ", \"title\": \"StoreDataConfig\"}");
}
But I am always getting java.util.regex.PatternSyntaxException: Illegal repetition.
Can you let me know what is the mistake?
If I don't use a method for [] and just giving it directly, I am not getting an error
It looks like you are doing this:
Take a valid regex for matching [].
Embed the regex in some JSON
Attempt to compile the JSON-with-an-embedded-regex as if the whole lot was a valid regex.
That fails ... because the JSON-with-an-embedded-regex is not a valid regex.
For a start, the { character is a regex meta character.
But the real puzzle is .... what are you actually trying to do here?
If you simply want a regex that matches a literal string then this will do it.
Pattern p = Pattern.compile(Pattern.quote(someLiteralString)).
And you could build a regex out of sub-regexes and literal strings by using Pattern.quote to escape the literal parts and then concatenating.
If what you are ultimately trying to do here is to extract information from a JSON string using pattern matching / regexes, then ... don't. The better approach is to use a proper JSON parser, and extract the information you need from the JSON object tree.
It's because you need to escape {} characters like this "\\{"
How replace all " characters with \". I have some String, like this:
{"ConfirmId":"0d8de6a6-15f1-4084-ba3a-4ebfe3151b38","Amount":1.00,"Fee":0.00,"FullAmount":1.0}
And I need something like this answer:
{\"ConfirmId\":\"0d8de6a6-15f1-4084-ba3a-4ebfe3151b38\",\"Amount\":1.00,\"Fee\":0.00,\"FullAmount\":1.0}
How can I do this?
This is example of my part of code:
String replace = Answer.get(1);
replace = "\"" + replace.replaceAll("\"","\\\"") + "\"";
System.out.println(replace);
But it doesn't work nice. Have somebody any idea?
Change your line to following
replace = replace.replaceAll("\"", "\\\\\"");
It will change "Confimed" to \"Confimed\"
I want to replace spaces from path string. I tried below but doesn't seems to be working :
String path = "/Users/TD/San Diego";
path=path.replaceAll(" ","\\ ");
System.out.println(path);
Goal is to convert
"/Users/TD/San Diego" to "/Users/TD/San\ Diego"
Any further space from string also needs to be replaced with "\ "
You could change
path = path.replaceAll(" ", "\\ ");
to escape the backslash
path = path.replaceAll(" ", "\\\\ ");
When I do that, I get (the requested)
/Users/TD/San\ Diego
Another option would be using String.replace like
path = path.replace(" ", "\\ ")
which outputs the same.
The suggested solution did not work for me (in Android Java).
So this is what I've came up with, after quite a few attempts:
path = path.replace(" ", (char) 92 + " ");
I have
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\\";
How can i replace
" to \"
and \ to \\
?
String result = a.replace("\"", "\\\"");
OR
String result = a.replace(""", "\"");
String result = a.replace("\\","\\\\").replace("\"", "\\\"");
This would first replace all \ with \\ and then all " with \" if that is what you want.
Note that doing it the other way round would result in " being replaced with \\" in the end, since first it get replaced with \" and then the \ would be replaced with \\ resulting in \\".
Additional note: your data string is not well-formed and should not compile: it ends in \" which is not a valid string literal delimiter (the literal ends in \\\\\" which would be the string data \\") - change that to an even number of slashes or add another " to the end in order to fix that.
The former. The latter is not well-formed Java code.
Since "String result = a.replace(""", "\"");" does not compile, does that answer your question?
you a string error,Less a quote
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\"";
System.out.println(a.replace("\"", "\\\""));