0

I am writing a function to add a backslash after every backslash in a string, for example, I want a string like this "C:\Users\acer\Desktop\Test.docx" to turn into this "C:\\Users\\acer\\Desktop\\Test.docx", but for some reason, I can't add backslashes anywhere except after the final backslash in the text.

   public static String validate(String text) {
    String t = text;
    String t1 = "";
    for (int i = 0; i < t.length(); i++) {
        if (t.charAt(i) == '\\') {
            t1 = t.substring(0, i) + "\\" + t.substring(i);
        }
    }
    t = t1;
    return t;
}

the output is the following: "C:\Users\acer\Desktop\\\Test.docx"

(used "C:\Users\acer\Desktop\Test.docx" as an example)

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    Try a debugger and step through the code - it looks like `t.substring(0, i)` will replace the the previous replacement which results in just the last replacement. See also: `String.ReplaceAll(...)` – Andrew S Jul 10 '22 at 17:00
  • I think this is because for one, you assign the value of the new string to t1 after you concatenate "\\", HOWEVER, you do not save the result to t, meaning t is unchanged original value. Because of this, during next iterations, if t.charAt(i) =='\\', it overwrites your changes – experiment unit 1998X Jul 10 '22 at 17:01
  • 1
    As a side note: In Java backslashes for file path is not generic; use plain slashes instead and each platform in which java runs can handle it accordingly. Cf. https://stackoverflow.com/a/2417546/5841551 – kladderradatsch Jul 10 '22 at 17:02
  • 3
    It looks like you are looking for something like `String replacedText = text.replace("\\", "\\\\");`, but at the same time it may also be [XY problem](https://meta.stackexchange.com/q/66377) so try to clarify why you need that code (how/when do you want to use it). – Pshemo Jul 10 '22 at 17:02
  • 1
    It feels like you're trying to reinvent the [replaceAll](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)) method that Strings come with. Why do you feel you need to do that? Also note that your method name does not reflect what the method code does: you're not validating anything, so I'd strongly recommend naming it after what it does instead (e.g. `escapePathString` or something). – Mike 'Pomax' Kamermans Jul 10 '22 at 17:08
  • 1
    Possibly related: [Replace backslash with double backslash](https://stackoverflow.com/q/15041111) – Pshemo Jul 10 '22 at 17:17

1 Answers1

1

With this simple code your problem should be solved, without using any loop.

public class Main {
    public static void main( String[] args ) {
        String path_singleSlash = "C:\\Users\\acer\\Desktop\\Test.docx";

        String path_doubleSlash = path_singleSlash.replaceAll( "\\\\", "\\\\\\\\" );

        System.out.println( "Single slash: " + path_singleSlash );
        System.out.println( "Double slash: " + path_doubleSlash );
    }
}

Output:

Single slash: C:\Users\acer\Desktop\Test.docx
Double slash: C:\\Users\\acer\\Desktop\\Test.docx

Let me know if it was helpful.

TheSmith1222
  • 345
  • 1
  • 4
  • 11