How to replace '\' with '//' in java String - java

For Example:
My String is:
C:\Users\Junaid\Documents\lesson2\lesson2\touchingRectangles\package.bluej
But I want it to be like this:
C://Users//Junaid//Documents//lesson2//lesson2//touchingRectangles//package.bluej

You can achieve the expected result with the following code -
path = path.replace("\\", "//");
Here the backslash() needs to be escaped by a backslash, as it throws invalid sequence error.
But honestly you don't need double front slash(//) unless you're handling anything like an URL(https://). Sometimes the filepath requires double blackslash(//) for file or stream inputs; but not always.
I would suggest you to use File.Separator instead. E.g.
String filePath = "hello" + File.separator + "world"
this gives you -"hello\world"; that is far more enough for file path manipulations.
Hope this helped :)

Related

Is their any way to eliminate two backward slash to one in Eclipse

I came to a scenario while using eclipse, in which if i use two back slash in below mention function.
"private Keywords(){
try{
OR=new Properties();
FileInputStream fs=new FileInputStream(System.getProperty("user.dir")+"**\\src\\com\\config\\OR.properties"**);
OR.load(fs);
"
this function works but if I use single slash it won't work . Is their way that i would be able to use single backward slash only while giving a source path..
Your question has nothing to do with Eclipse.
You need to escape back-slashes in Strings, as they are themselves an escape character.
What you can eventually use to somewhat "shorten" your code is the system property System.getProperty("file.separator"), then assign it to some constant and use that reference instead.
But that's close to cosmetics.
You can use the 2 backslashes as a single variable say,
String separator = "\\";
String file_path = "src"+separator +"com"+separator +"config"+separator +"OR.properties";
System.out.println("File Path is :: " + file_path);
Or as Mena Suggested, you can use:
String separator = System.getProperty("file.separator");
Get it straight with your String literals
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html

Replace "\\" with "/" in Java

I am trying to replace '\\'with '/' in java(Android) and this does not seem to work!
String rawPath = filePath.replace("\\\\", "/");
What is wrong with this ? I have escaped "\" and tried escaping '/' but to no use. Nothing happens to the original string.
filePath = abc\\xyz(not after escaping two \\, the original string is with two \\)
rawPath = abc \ xyz
expected = abc/xyz
Whats the correct way of doing this? (Another Windows file to Android path conversion prob)
When using String.replace(String, String) the backslash doesn't need to be escaped twice (thats when using replaceAll - it deals with regex). So:
String rawPath = filePath.replace("\\", "/");
Or using char version:
String rawPath = filePath.replace('\\', '/');
You do not need the quad-druple escape,
\\\\
, just simply
\\
.
escape with single slash should be enough. Following is working fine for me.
String rawPath = filePath.replace("\\", "/");
public static void main(String[] args) {
String s = "foo\\\\bar";
System.out.println(s);
System.out.println(s.replace("\\\\", "/"));
}
will print
foo\\bar
foo/bar
If you want to replace a sequence of 2 backslashes in your original string with a single forward slash, this should work:
String filePath = "abc\\\\xyz";
String rawPath = filePath.replace("\\\\", "/");
System.out.println(filePath);
System.out.println(rawPath);
outputs:
abc\\xyz
abc/xyz
Do you really have two backslashes in the String in the first place? That only appears in Java source code. At runtime there will only be one backslash. So the task reduces to changing backslashes to forward slashes (why?). For which you need a regex if you are using replaceAll(), which would require four of them: two for the compiler, and two for the regex, but you aren't using that, you are using replace(), which isn't a regex, so you only need two, one for the compiler and one for itself.
Why are you doing this? It is never necessary to use a backslash in a File path in Java at all, and it is also never necessary to translate them to / unless you are doing URL-like things with them, in which case there are File.toURI() methods and URI and URL classes for that.
Here is a very small method to get the desktop path and show you how to replace them in the return statement.
public static String getDesktopPath() {
String desktopPath = System.getProperty("user.home") + "/Desktop";
return desktopPath.replace("\\", "/");
}

How to format system path to a file in java?

I want to read a file say c.txt in java in windows. So can anybody suggest me that how can I format a system path to a file say D:\a\b\c.txt to D:/a/b/c.txt in java? I know it will work like this D:\\a\\b\\c.txt but I want to use this D:/a/b/c.txt. Thanks!
I'm not sure of your problem but rarely is it good practice to hard code / or \. Use Java's File.separator to help you.
You could use the char replace: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace%28char,%20char%29
Example:
String pathToFile = "D:\\a\\b\\c.txt";
pathToFile = pathToFile.replace('\\','/'); <-- with ' and not "
Documentation of replace(char, char):
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar.
You can use File API
File f = new File("c.txt");
System.out.println(f.getAbsolutePath());
System.out.println(f.getCanonicalPath());
or just simply substring
String fname = "D:\\a\\b\\c.txt".replace('\\', '/');
System.out.println(fname);
String file="D:\\a\\b\\c.txt";
file=file.replace('\\','/');
System.out.println(file);
output D:/a/b/c.txt
But if you are trying to make it more platform dependent you should use File.separator (for replacement based on Strings) or File.separatorChar (for replacement based on chars).

Splitting filenames using system file separator symbol

I have a complete file path and I want to get the file name.
I am using the following instruction:
String[] splittedFileName = fileName.split(System.getProperty("file.separator"));
String simpleFileName = splittedFileName[splittedFileName.length-1];
But on Windows it gives:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
Can I avoid this exception? Is there a better way to do this?
The problem is that \ has to be escaped in order to use it as backslash within a regular expression. You should either use a splitting API which doesn't use regular expressions, or use Pattern.quote first:
// Alternative: use Pattern.quote(File.separator)
String pattern = Pattern.quote(System.getProperty("file.separator"));
String[] splittedFileName = fileName.split(pattern);
Or even better, use the File API for this:
File file = new File(fileName);
String simpleFileName = file.getName();
When you write a file name, you should use System.getProperty("file.separator").
When you read a file name, you could possibly have either the forward slash or the backward slash as a file separator.
You might want to try the following:
fileName = fileName.replace("\\", "/");
String[] splittedFileName = fileName.split("/"));
String simpleFileName = splittedFileName[splittedFileName.length-1];
First of all, for this specific problem I'd recommend using the java.util.File class instead of a regex.
That being said, the root of the problem you're running into is that the backslash character '\' signifies an escape sequence in Java regular expressions. What's happening is the regex parser is seeing the backslash and expecting there to be another character after it which would complete the escape sequence. The easiest way to get around this is to use the java.util.regex.Pattern.quote() method which will escape any special characters in the string you give it.
With this change your code becomes:
String splitRegex = Pattern.quote(System.getProperty("file.separator"));
String[] splittedFileName = fileName.split(splitRegex);
String simpleFileName = splittedFileName[splittedFileName.length-1];
Another simpler way could be to do
File f = new File(path);
String fileName = f.getName();
I believe this will work provided the paths are compatible with the platform, i.e. not sure if path "c:\file.txt" will work on Linux or not.

Replace backslashes in a string using Java/Groovy

Trying to get a simple string replace to work using a Groovy script. Tried various things, including escaping strings in various ways, but can't figure it out.
String file ="C:\\Test\\Test1\\Test2\\Test3\\"
String afile = file.toString() println
"original string: " + afile
afile.replace("\\\\", "/")
afile.replaceAll("\\\\", "/") println
"replaced string: " + afile
This code results in:
original string: C:\Test\Test1\Test2\Test3\
replaced string: C:\Test\Test1\Test2\Test3\
----------------------------
The answer, as inspired by Sorrow, looks like this:
// first, replace backslashes
String afile = file.toString().replaceAll("\\\\", "/")
// then, convert backslash to forward slash
String fixed = afile.replaceAll("//", "/")
replace returns a different string. In Java Strings cannot be modified, so you need to assign the result of replacing to something, and print that out.
String other = afile.replaceAll("\\\\", "/")
println "replaced string: " + other
Edited: as Neftas pointed in the comment, \ is a special character in regex and thus have to be escaped twice.
In Groovy you can't even write \\ - it is "an unsupported escape sequence". So, all answers I see here are incorrect.
If you mean one backslash, you should write \\\\. So, changing backslashes to normal slashes will look as:
scriptPath = scriptPath.replaceAll("\\\\", "/")
If you want to replace pair backslashes, you should double the effort:
scriptPath = scriptPath.replaceAll("\\\\\\\\", "/")
Those lines are successfully used in the Gradle/Groovy script I have intentionally launched just now once more - just to be sure.
What is even more funny, to show these necessary eight backslashes "\\\\\\\\" in the normal text here on StackOverflow, I have to use sixteen of them! Sorry, I won't show you these sixteen, for I would need 32! And it will never end...
If you're working with paths, you're better off using the java.io.File object. It will automatically convert the given path to the correct operating-system dependant path.
For example, (on Windows):
String path = "C:\\Test\\Test1\\Test2\\Test3\\";
// Prints C:\Test\Test1\Test2\Test3
System.out.println(new File(path).getAbsolutePath());
path = "/Test/Test1/Test2/Test3/";
// Prints C:\Test\Test1\Test2\Test3
System.out.println(new File(path).getAbsolutePath());
1) afile.replace(...) doesn't modify the string you're calling it on, it just returns a new string.
2) The input strings (String file ="C:\\Test\\Test1\\Test2\Test3\\";), from Java's perspective, only contain single backslashes. The first backslash is the escape character, then the second backslash tells it that you actually want a backslash.
so
afile.replace("\\\\", "/");
afile.replaceAll("\\\\", "/");
should be...
afile = afile.replace("\\", "/");
afile = afile.replaceAll("\\", "/");
In Groovy you can use regex in this way as well:
afile = afile.replaceAll(/(\\)/, "/")
println("replaced string: "+ afile)
Note that (as Sorrow said) replaceAll returns the result, doesn't modify the string. So you need to assign to a var before printing.
String Object is immutable so if you call a method on string object that modifies it. It will always return a new string object(modified). So you need to store the result return by replaceAll() method into a String object.
As found here, the best candidate might be the static Matcher method:
Matcher.quoteReplacement( ... )
According to my experiments this doubles single backslashes. Despite the method name... and despite the slightly cryptic Javadoc: "Slashes ('\') and dollar signs ('$') will be given no special meaning"

Categories

Resources