Kurze Frage zu replace()

Förster

Mitglied
Guten Morgen allerseits,
wahrscheinlich steh ich mächtig auf dem Schlauch aber ich versteh gerade nicht wieso das nicht funktionier:
Java:
selectedFile = selectedFile.toString().replaceAll("\" ,"/");

Was muss ich einsetzten damit \ ersetzt wird?

Vielen Dank im voraus!
 
Die Zeile von dir sollte eigentlich einen Compile-Fehler verursachen:

Mit einem Schrägstrich \ escapt man in Javastrings Sonderzeichen. Wenn du in einem String wirklich ein \ haben willst, musst du \\ schreiben (einen zum escapen und einen "echten").

Außerdem ist replaceAll für Regex-Ausdrücke gedacht. in deinem Fall würde ich also eher replace verwenden, sonst musst du den Schrägstrich nochmal für Regex Escapen und bräuchtest \\\\.

Java:
selectedFile = selectedFile.toString().replace("\\" ,"/");
 
Vielen Dank erstmal für eure schnelle Antwort.

Ich habe das ganze mal so probiert wie @Robertop es geschrieben hat. Hat leider nicht funktioniert.
Ich hol mal etwas weiter aus wieso nicht:

Ziel ist es einen Path zu String zu machen um mir eine csv datei in eine Liste zu schreiben und zu verarbeiten.

Tatsächlich hat es mit replaceAll() funktioniert und den \\\\.

Das ganze ist zwar umständlich sieht jetzt aber so aus:
Java:
if (result == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
}

Und dann:

Java:
selectedPath = selectedFile.toPath();
String pathString = selectedPath.toString();
pathString = pathString.replaceAll("\\\\","/");

Zumindest funktioniert es, ich danke!
 
Java:
selectedPath = selectedFile.toPath();
String pathString = selectedPath.toString();
[CODE=java]pathString = pathString.replaceAll(File.separator,"/");
wie wäre es mit File.separator anstatt "\\\\" ?
Code:
pathString = pathString.replaceAll(File.separator,"/");
 
wie wäre es mit File.separator anstatt "\\\\" ?
Code:
pathString = pathString.replaceAll(File.separator,"/");
Das wird nicht funktionieren, da replaceAll ein Regulären Ausdruck erwartet und da ist der \ auch wieder nur ein escape Zeichen. Daher ja die zweite Verdoppelung.

Die Frage ist maximal, wozu er das braucht. Was wird mit dem Wert weiter gemacht?
 
Das wird nicht funktionieren, da replaceAll ein Regulären Ausdruck...
Doch!
Java:
public class Main {
    public static void main(String[] args) {
       String path = "/usr/bin/java";
       String tmp = path.replaceAll(java.io.File.separator, "|");
       System.out.println(path+"\n"+tmp);
    }
}
 

Anhänge

  • main.jpg
    main.jpg
    12,8 KB · Aufrufe: 2
Also JVM ist doch nicht immer gleich JVM zwischen Linux und Windows? So ist es wahrscheinlich auch mit dem Problem
Die JVM ist schon gleich. Das Problem ist hier lediglich, dass File.separator ein Zeichen sein kann, dass in Regulären Ausdrücken eine besondere Bedeutung haben kann. Das ist bei Windows z.B. der Fall, denn dort ist das Zeichen hat der Backslash und der ist in regulären Ausdrücken auch das Escape Zeichen. Daher bräuchte man in dem regulären Ausdruck zwei davon. Und das macht dann in einem String Literal 4 Backslash um eben ein String mit zwei Backslash zu bekommen.
 
Die Problematik tritt nicht bei Unix Trenner auf sondern nur bei dem Windows Trenner, denn dort ist dieser ist der Backslash und der hat in regulären Ausdrücken eine besondere Bedeutung. Probiere es also einfach unter Windows aus. Oder glaube @Robertop, der darauf schon in #2 hingewiesen hat.
Das geht auch bei Windows

Java:
class Scratch {
    public static void main(String[] args) {
        String path = "C:\\tmp\\test";
        String tmp = path.replace(File.separator, "|");
        System.out.println(path+"\n"+tmp);
    }
}

Code:
C:\tmp\test
C:|tmp|test

/Edit: natürlich quatsch, es geht um replaceAll.... da gehts nicht
 
You scratch my back and I'll scratch your back, huh?🤣 I'm not against IDE like you say, but I don't let IDE rule me and tell me what to do. Maybe you would say that this old Joe is too backward. Nope! Let me tell you this: Self-driving cars are currently en vogue for the wannabe modern. But if you have to work with AI, you will see the limitations of AI. Even the most touted ChatGPT is mocked (as smart as a 6 year old toddler). Google gives its developers a gag order to babble out the "Bard" project (an AI chatbot). Why? Because Bard is more unpredictable than the most spoiled 6-year-old toddler. Back to the IDE/Self-Driving question: Would you rather be backward and take your life in your own hands THAN let AI drive the car and put your life in the hands of an unknown AI bot?
 
but I don't let IDE rule me and tell me what to do.
Sorry, but I do not understand that.

The IDE does not tell me, what to do. You can configure it. You the IDE is not telling me what to do but it tells me, when I did something wrong. That is simple QA! When you produce something, then you will have some QA that checks the result. So I configure the IDE so that it helps me to do, what I want to do. But that simply requires that you really think about how everything should be. It does not matter, what you create / produce: You should also think about what you are building and what quality you want to build. It is ok to ignore that when you just play around but as soon as you want to build some kind of product, then this is really required.

And the IDE is the tool that is only doing some really easy stuff because it is not capable to do a lot (it can change with plugins. I testes some quite nice pugins in the past but it wasn't worth the money because I was to lazy to work on the configuration and the additional value wasn't there because of the tools I already have in place in my workflows). Most important are:
  • all the Tests (As software developer, that are more likely Unit Tests)
  • lots of tools that are doing checks: Static Code analysis like PMD, Spotbugs but also AI based stuff like snyk that find much more.

But that are just tools. OWASP (just to name an example) is something that is really important for all software engineers in my eyes. And the most important part is of course the developer that needs to understand:
  • why is this important
  • what should he do. (If some developer must be forced to follow best practices then this developer is not a professional developer. Sorry, but such a person would be fired from all project teams that I know of! So this is really something that the developer should want. And then the tools are just helping him. It is not a tool that "tells him what he has to do")

But that is just my view. I do not want to convience you. You have your optinion and you are quite happy with it and that is great. This is mostly for others that read this thread to get a different view, too. (I don't want to say, that this view is better or correct. But I can say that multiple companies have exactly this view! So "Secure Software Engineering" is a mandatory training session once per month for all developers in our department - I am working in a department that is a pure software engineering department so it is simply mandatory for all employees except the few people who onlay have management stuff to do!)
 
The IDE does not tell me, what to do. You can configure it. You the IDE is not telling me what to do but it tells me, when I did something wrong
🤣Congratulation.
Have you ever known how desperate newbies were and still are with IDE. Beginning with the downloading and end up with compiler installation. Especially with JAVA. IDE sets and manages the CLASSPATH and PATH still and correctly so that a newbie feels great and think he's great. But outside his or her IDE environment he or she becomes a dead chicken.

My wife were a former teacher (now retired) once said to me: "you know, in a class of 20 students. Maybe there are one or two are good and they lift the level of the class". You are probably the one or two and you lift the level of this forum.

Pls. let's stop discussing about IDE. The Germans used to say "Über Geschmack lässt sich nicht streiten."
 
Zuletzt bearbeitet von einem Moderator:
Generell gilt ja:
A fool with a tool is still a fool
und
If your only tool is a hammer every problem looks like a nail

Das trifft auch auf IDEs und alle Tools zu - sie sind unverzichtbar in vielen Konstellationen, müssen aber sinnvoll eingesetzt werden und das tun was sie sollen und einen nicht behindern (I'm looking at you Eclipse!)
 

Neue Themen


Zurück
Oben