-3

Using browse fuctionality, I have got the filepath into the String fileName.

 FileItem fi =(FileItem)i.next();
    String fileName = fi.getName();

C:\Users\ramya_varakantham\Desktop\juno\Servlet\20-06-15.csv is the path that I have got into fileName.

I now want to replace \ with \\ so that my output would be C:\\Users\\ramya_varakantham\\Desktop\\juno\\Servlet\\20-06-15.csv

Please let me know how.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307

2 Answers2

0

Use RegExp or String replace method if you really want.

Kishore Reddy
  • 886
  • 3
  • 11
  • 19
0

Try this (String replacement):

String s = "C:\\Users\\ramya_varakantham\\Desktop\\juno\\Servlet\\20-06-15.csv";
s = s.replace("\\", "\\\\");
System.out.println(s);

or this (Regex replacement)

String s = "C:\\Users\\ramya_varakantham\\Desktop\\juno\\Servlet\\20-06-15.csv";
s = s.replaceAll("\\\\", "\\\\\\\\");
System.out.println(s);

output in both cases is:

C:\\Users\\ramya_varakantham\\Desktop\\juno\\Servlet\\20-06-15.csv
Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • `replaceAll` - do we really need regex here? – Pshemo Sep 25 '15 at 12:52
  • I don't blame him for using `replaceAll`. It's kind of weird naming on Oracle's part to call it `replaceAll` when in fact it does the same as just `replace` - just with regex. I actually only found out a few months ago that `replace` should be used for replacing (all) occurences of a CharSequence instead of `replaceAll` - the name implies something different. – nbokmans Sep 25 '15 at 12:56
  • Well we don't HAVE to, we could use this one, as well: `s.replace("\\", "\\\\");` but who cares? The result is the same and performance is not an issue here. – Würgspaß Sep 25 '15 at 12:58
  • @Würgspaß "but who cares?" well, people who are familiar with regex in Java will be able to read this code, but even them would probably prefer to use `replace("\\", "\\\\")` for simplicity since it is easier to read for everybody and it is less error-prone (it also allows us to avoid problems when people will want to replace string containing regex metacharacters like `.` `+` and so on, just because they seen someone else using `replaceAll`). In other words if you don't need regex support prefer `replace` over `replaceAll`. – Pshemo Sep 25 '15 at 13:02
  • Edited. I mostly need regex replacement, so I am more familiar with `replaceAll` – Würgspaß Sep 25 '15 at 13:10
  • Only difference between `replace(String,String)` and `replaceAll(String,String)` is regex syntax support. Both methods are using regex engine internally, but `replace(String,String)` automatically adds escaping for us. It is something like `replaceAll(Pattern.quote(target), Matcher.quoteReplacement(replacement))` which makes it perfect for literal replacements. Only `replace(char,char)` is truly regex-less because it doesn't even use regex engine internally, it simply iterates over array of characters and replaces them when needed. – Pshemo Sep 25 '15 at 13:16
  • @nbokmans I agree `replaceAll` name sucks. Even `replaceRegex` would be better name (although still not perfect) since it would show only difference between `replace` and `replaceAll` without causing confusion which having `replaceFirst` and `replaceAll` creates, where lack of `All` suffix may suggest that `replace` will not guarantee to replace all instances of text/char we want to replace. – Pshemo Sep 25 '15 at 13:23