Autor Beitrag
LocalDeeJay
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows XP, Windows Vista Ultimate
C/C++ (VS6.0, VS 2005), C# (VS 2005) , PHP (Adobe Dreamweaver CS3, Apache)
BeitragVerfasst: So 18.11.07 18:47 
Hallo meine lieben Kollegen,

ich habe mal wieder eine Frage:
Wie setze ich ein Zeichen in einen String ein?

Ein Beispiel:
Ich habe einen Pfad, z.B. dokumenteundeinstellung\user\desktop\music's.mp3
Angenommen ich möchte den Pfad dieser Datei in eine SQL-Datenbank einfügen. So muss ich dafür sorgen das vor dem Apostroph ein Backslash \ gesetzt wird.

Ich hab nun einige Funktionen von .NET, komme aber nicht wirklich damit klar...

Kann mir jemand weiterhelfen? Wäre sehr nett.


Moderiert von user profile iconChristian S.: Topic aus Allgemeine .NET-Fragen verschoben am So 18.11.2007 um 17:50

_________________
Thomas Müller
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 18.11.07 18:52 
Würd ich einfach über die Replace-Methode des Strings machen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
LocalDeeJay Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows XP, Windows Vista Ultimate
C/C++ (VS6.0, VS 2005), C# (VS 2005) , PHP (Adobe Dreamweaver CS3, Apache)
BeitragVerfasst: So 18.11.07 18:56 
Hat ich ja auch schon versucht...

Erstmal hallo Christian =)

Die syntax wär halt bissl komisch... Ich habe ein Zeichen (den Apostrophen), den ich dann durch zwei Zeichen (Backslash & Apostroph) ersetzen müsste. Mit Char geht das nicht, da es zwei zeichen sind. So müsste ich die 2. überladene Funktion nehmen - also die zwei Stringparameter.
Und wie soll das bitte aussehen?!

ausblenden C#-Quelltext
1:
strTag = strData.replace("'","\'");					


so gehts leider net.

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt

_________________
Thomas Müller
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 18.11.07 19:11 
Du musst dran denken, dass \ auch in C#-Strings ein Escape-Charakter ist und daher selber nochmal escaped werden muss -> "\\'"

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
LocalDeeJay Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows XP, Windows Vista Ultimate
C/C++ (VS6.0, VS 2005), C# (VS 2005) , PHP (Adobe Dreamweaver CS3, Apache)
BeitragVerfasst: So 18.11.07 19:17 
Hey cool - danke!
Es funktioniert.
Wobei sich mir eine Frage stellt. Wenn ich ' will müsste ich \' machen, wenn ich \ will müsste ich \\ machen. Wenn ich aber \' will, wieso muss ich dann nicht \\\' machen? Ich mache ja eben \\', was entweder einem Backslash und Escape-Squenz ' entspricht oder was der Escape-Sequenz \ entspricht und ' oder...?! Ach ist zu hoch für mich. Zu viele Backslashs und Apostrophe... *gg*

_________________
Thomas Müller
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 18.11.07 19:22 
user profile iconLocalDeeJay hat folgendes geschrieben:
Wenn ich ' will müsste ich \' machen,
Nein, da reicht dann "'". Ist ja kein Escape-Character!

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
LocalDeeJay Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows XP, Windows Vista Ultimate
C/C++ (VS6.0, VS 2005), C# (VS 2005) , PHP (Adobe Dreamweaver CS3, Apache)
BeitragVerfasst: So 18.11.07 19:25 
Ahh, hehe

Klar, bei SQL hab ich das ' als Escape, weil ich damit die Werte bezeichne. Und in normalem .NET ist das ' also ein normaler apostroph und nur die anderen Dinge, wie \\ oder \" sind dann die Escapesequenzen... naja dann klingts doch irgendwie logisch wenn das so ist =)

Vielen Dank, hast mir mal wieder weitergeholfen ;)


thommy

_________________
Thomas Müller
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 18.11.07 20:23 
Das hat nix mit .Net zu tun, das ist einfach nur eine Definition von C# ;) .
LocalDeeJay Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows XP, Windows Vista Ultimate
C/C++ (VS6.0, VS 2005), C# (VS 2005) , PHP (Adobe Dreamweaver CS3, Apache)
BeitragVerfasst: So 18.11.07 22:11 
Sry, ich wollte nur versuchen, die verschiedenen Ausdrücke von C# und SQL klar zu deuten.

_________________
Thomas Müller