I had a scenario where I need to give back slash for a key in JSON put method like below
json.put("path" , " \\abx\2010\341\test.PDF");
The value I gave for path key shows error.
How to handle this case?
You need to write double slash instead of one: \\
So your code become:
json.put("path" , " \\\\abx\\2010\\341\\test.PDF");
You can learn more about escaping special characters in this answer.
Try like this
json.put("path" , "\\abx\\2010\\341\\test.PDF");
You need to escape \.
Try json.put("path" , " \\abx\\2010\\341\\test.PDF");
You can learn more about it under Escape Sequences.
double \\ = single \ in string ""
json.put("path","\\abx\\2010\\341\\test.PDF");
If you want to show like this, JSON file should be as below.
"path" : " \\abx\2010\341\test.PDF"
JSON also \ represent as \ in java special characters. Then java code should be as below.
json.put("path" , " \\\\abx\\2010\\341\\test.PDF");
Related
I am parsing below string value into OData query through java code.
objects.put("EndDate", "\/Date(1441756800)\/";
How can i parse the /Date(1441756800)/ into a string in java.
I have tried with below :
objects.put("EndDate", ""\\""//"Date(1441756800)""\\""//"";
throws error:(
I never used OData so I may not understand your question correctly, but if you are asking how to write \/Date(1441756800)\/ as String then you need to escape \ as it is String special character (used for instance when escaping or when creating other special characters like line separators \n).
So try with "\\/Date(1441756800)\\/"
Try this - objects.put("EndDate", "'Date(1441756800)'";
I have a file with the following contents
PackId1;ChannelId1;SalesCode1;AccountNumber1
PackId2;ChannelId2;SalesCode2;AccountNumber2
PackId3;ChannelId3;SalesCode3;AccountNumber3
PackId4;ChannelId4;SalesCode4;AccountNumber4
.
.
.
.
.
PackId10;ChannelId10;SalesCode10;AccountNumber10
I wrote code to read the first line of the above file. Now, after the line is read i want to replace all the semicolon(;) to hash(#) or some other special character so that next time the same line is not read.
Please help me in replacing the string (;) to (#) using java/groovy.
Tried replaceFirst() as below, but it didn't work for me
str = str.replaceFirst("\\\;" , "\\\\#")
No need to use regexes here, like replaceFirst() does. Use replace() instead:
str = str.replace(";" , "#")
What didn't work?
'PackId1;ChannelId1;SalesCode1;AccountNumber1 PackId2;ChannelId2;SalesCode2;AccountNumber2'.replace(';','#')
I have a string.
String invalid = "backslash escaping as <>:;%+\/"."
I received an error message telling me to add \ to escape the sequence.
When I try to write this in Java I know that backslash needs to be escaped as \\. So I wrote it as:
String invalid = "backslash escaping as <>:;%+\\/\"."
Now this displays as backslash escaping as <>:;%+\\/".
The backslash is not escaping. How do I get only one backslash?
I don't find a problem with your modification. This runs as expected:
String invalid = "backslash escaping as <>:;%+\\/\".";
System.out.println(invalid);
output:
backslash escaping as <>:;%+\/".
In your first example, you have a little problem:
\/".
Which I believe should be like this:
/\".
Because in the first way, you are closing the string before you want to. The part ." is out of the String.
Edit:
try writing the output to a file or to a JTextField or something and see what happens, your string is correct and if you compare your output with my output it is the same. It might be an issue with your debugger (weird, but possible).
The second string in your post is correctly displayed. There must be something wrong with the way in which you observe the output.
Just try the simplest thing possible:
Create a file named Escape.java and write this code into its contents:
public class Escape {
public static void main(String... args) {
String s = "backslash escaping as <>:;%+\\/\".";
System.out.println(s);
}
}
Open a whatever command line that your OS provides and go to the folder with the said source file.
Compile the source file:
javac Escape.java
And run the class file:
java -cp . Escape
It should give this output:
backslash escaping as <>:;%+\/".
...which is exactly what you want, I believe.
I have done my .java file that changes registry data. But I am getting "illegal escape character" error on the line where Runtime.getRuntime().exec exists. Where is my mistake ?
import java.util.*;
import java.applet.Applet;
import java.awt.*;
class test {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("REG ADD 'HKCU\Software\Microsoft\Internet Explorer\Main' /V 'Start Page' /D 'http://www.stackoverflow.com/' /F");
} catch (Exception e) {
System.out.println("Error ocured!");
}
}
}
You need to escape the backslashes used in your path.
String windowsPath = "\\Users\\FunkyGuy\\My Documents\\Hello.txt";
You need to escape \ with another \, so replace \ with \\ in your input string.
You need to escape the backslash characters in your registry path string:
"REG ADD `HKCU\\Software\\ ...
The backslash character has a special meaning in strings: it's used to introduce escape characters. if you want to use it literally in a string, then you'll need to escape it, by using a double-backslash.
Back slashes in Java are special "escape" characters, they provide the ability to include things like tabs \t and/or new lines \n and lots of other fun stuff.
Needless to say, you to to "escape" them as well by adding an addition \ character...
'HKCU\\Software\\Microsoft\\Internet Explorer\\Main'
On a side note. I would use ProcessBuilder or at the very least, the version of Runtime#exec that uses array arguments.
It will save a lot of hassle when it comes to dealing with spaces within command parameters, IMHO
Probably because you didn't escape the backslash in your string. Have a look at http://docs.oracle.com/javase/tutorial/java/data/characters.html for more information about proper escaping.
you need replace escape \ with \\
below code will work
Runtime.getRuntime().exec("REG ADD 'HKCU\\Software\\Microsoft\\Internet Explorer\\Main' /V 'Start Page' /D 'http://www.stackoverflow.com/' /F");
I have to replace \\ with \ in Java. The code I am using is
System.out.println( (MyConstants.LOCATION_PATH + File.separator + myObject.getStLocation() ).replaceAll("\\\\", "\\") );
But I don't know why it is throwing StringIndexOutOfBoundsException.
It says String index out of range: 1
What could be the reason? I guess it is because the first argument replaceAll accepts a pattern. What could be the possible solution?
Stacktrace
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:558)
at java.util.regex.Matcher.appendReplacement(Matcher.java:696)
at java.util.regex.Matcher.replaceAll(Matcher.java:806)
at java.lang.String.replaceAll(String.java:2000)
Answer Found
asalamon74 posted the code I required, but I don't know why he deleted it. In any case here it is.
There is a bug already filed in Java's bug database. (Thanks for this reference, asalamon.)
yourString.replaceAll("\\\\", "\\\\");
Amazingly, both search and replace string are the same :) but still it does what I require.
Use String.replace instead of replaceAll to avoid it using a regex:
String original = MyConstants.LOCATION_PATH + File.seperator
+ myObject.getStLocation();
System.out.println(original.replace("\\\\", "\\"));
Personally I wouldn't do it this way though - I'd create MyConstants.LOCATION_PATH_FILE as a File and then you could write:
File location = new File(MyConstants.LOCATION_PATH_FILE,
myObject.getStLocation());
which will do the right thing automatically.
Well, i tried
String test = "just a \\ test with some \\\\ and others \\\\ or \\ so";
String result = test.replaceAll("\\\\", "\\\\");
System.out.println(test);
System.out.println(result);
System.out.println(test.equals(result));
and got, as expected
just a \ test with some \\ and others \\ or \ so
just a \ test with some \\ and others \\ or \ so
true
What you really need is
string.replaceAll("\\\\\\\\", "\\\\");
to get
just a \ test with some \\ and others \\ or \ so
just a \ test with some \ and others \ or \ so
false
You want to find: \\ (2 slashes)
which needs to be escaped in the regex: \\\\ (4 slashes)
and escaped in Java: "\\\\\\\\" (8 slashes)
same for the replacement...
For the regex, if you want to change \ to \\, you should do this:
if (str.indexOf('\\') > -1)
str = str.replaceAll("\\\\", "\\\\\\\\");
str = "\"" + str + "\"";
Where \\\\ means \ and \\\\\\\\ means \\.
File.seperator is already escaped as is any string object so you are escaping them twice.
You only need to escape values that you are entering as a string literal.
The best way is :
str.replace(**'**\\**'**, **'**/**'**); //with char method not String
Try this
cadena.replaceAll("\\\\","\\\\\\\\")
I suspect the problem is that replaceAll() uses regexps and the backslash is an escape character in regexps as well as in Java - it might be necessary to double the number of backslashes.
In general you should always post the full stack trace of exceptions, it is much easier to diagnose the problem that way.
I believe what you need to do is:
System.out.println( (MyConstants.LOCATION_PATH + File.separator + myObject.getStLocation() ).replaceAll("\\\\\\\\", "\\\\") );
The regular expression String is actually four backslashes, which is a regular expression that matches two backslashes.
The replacement String has to be four slashes as per Java documentation, from:
http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String)
Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(str);
char character = iterator.current();
while (character != CharacterIterator.DONE )
{
if (character == '\\\\') {
result.append("\\");
}
else {
result.append(character);
}
character = iterator.next();
}
System.out.print(result);