Replace double backslashes with single backslash - c#

I have created a search function within a media player that uses a list-box to output the data and when a user clicks a song I want that song to play. However, when I select a song for some reason I get two black slashes instead of one. Please help. I've tried replacing them already.
string path = #"C:\Users\Username\Music\";
path = path.Replace(#"\\", #"\");
string selectedsong, filetoplay;
selectedsong = listBox1.SelectedItem.ToString();
filetoplay = path + selectedsong + ".mp3";
Form1.wplayer.URL = filetoplay;
What I'm currently getting at the moment is C:\\Users\\Username\\Music\\Song.mp3 and as a result the song won't play

That Replace in your code is doing nothing because there are no double backslashes in your string... As others have already pointed out, it's just a matter of debugger visualization so you can copy that and use it in your code, for example.
So, if you do this:
You see the double backslash there but it's not in the actual string, of course. See what you get in the console:
My advice is that you simulate a double click on the selected file by running this:
System.Diagnostics.Process.Start(filetoplay);
It should open your default mp3 player. I think it will give you an error due to missing file, wrong format or something. If it plays, then the bug is on the player part of your code and you can stop worrying about the double slashes. :)

It shows two backslashes in the variable value because the ­\ is escaped. If you print the variable value to console, you should find that it has only one backslash.

Try to use Path.Combine
link to msdn.microsoft.com
P.S. Single \ is "escape sign" and is used with "special charakters" ex: "\t" is "tab" and "\n" is "new line", that means, when you want to post "\" in your string, you must post "\".
P.S.2 When you are creating "path" use "Patch.Combine".

Related

read a path from file in a correct syntax with streamreader

I'm completely a junior here. I have tried something like
save a path and file string in a file like:
c:\aaa\bbb\text.txt
then I need to read again as path but I get c:\aaa\bbb\text.txt from streamreader, but I need c:\\\aaa\\\bbb\\\text.txt
Can anyone help me?
I think you might be confusing string literals with a string.
Say I write var myString = "\\" or var myString = #"\", this will show in the debugger as \\, because the debugger will format it as a literal. But if print it to the console, a file, or press the magnifying glass next to the string in the debugger, it will be shown as \, because that is the actual string value. See also verbatim string literal
So, if you do myStreamWriter.Write("c:\\aaa\\bbb\\text.txt");, you will be actually saving the string c:\aaa\bbb\text.txt, and that is also the string that will be read back.
However I fail to understand why you would want three slashes, I can only assume the OP thinks the escaping is done multiple times.

How to put quote (") in a string c#

I want to print something like : Welcome "Aditya"
I tried using escape character
string s = "Welcome \"Aditya\"";
But it is printing as : Welcome \"Aditya\"
I have even tried to just print backslash(\) using string s = "\\" but instead I got \\.
Can anyone help me understand why escape character is not working or if I am doing anything wrong here?
I'm guessing you're looking at the Watch window in Visual Studio:
The Watch window adds its own quotes, and puts backslashes before any quotes in the string. This is probably confusing you.
You can click the magnifying glass next to the string in the watch window to see its actual value:
Alternatively, put ,nq (for "no quotes") after the variable name:
A full list of Debugger Format specifiers is here.
If you're just hovering over the variable name, you can also click the magnifying glass to see the string's actual value:
Just use string literal:
string s = #"Welcome ""Aditya""";

How to use extra quotations in a regex statement in c#

Below I have a regex statement I have been working on for quite awhile. The problem I am having is that their are a lot of quotations I am trying to parse out (I think that's the terminology I am looking for) so Visual Studio is freaking out about it. I have tried to fix this using escape characters, but it still won't recognize the whole phrase.
Here is the phrase without the escape characters:
string exceptionPattern = #"(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))";
With just this code in, nothing else, almost every line in my code gets affected. Here is the code using escape characters:
string exceptionPattern = #"(?:(?:\"([^\"}]*)\")|(\w+))\s*:\s*(?:(?:\"([^\"}]*)\")|(\w+))";
Once this comes into play, only this line is not working. In VS, the ([^\ part close to the beginning is not highlighted, meaning that it is not in quotes. Does anyone have any idea on how I can fix this problem?
This is the string I am trying to match. Note: THIS IS NOT JSON! I have confirmed it many times with the developer who made the database where this sample is coming from and he confirmed it is not JSON, so please do not try to use JSON on this. Also, the regex I have is trying to match displayException and the message after it, and also exception and the message after that including success false, using the quotes as a point of splitting
{"data":"","displayException":"Invalid Account Status. Please complete the registration process by clicking the verification link in your eTTek Dash Registration Verification email. Please contact 1-800-341-6184 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN","success":false}
Inside a verbatim string, to escape a double quotes you must need to add another double double quotes near to that like "". So the compiler treats "" as a double quotes or otherwise it would treat " as an end of the verbatim string.

How to produce a soft return using C#.net

I know this is kind of easy question but i cant seem to find it anywhere. Is there someone out there who knows how to create a soft return inside a set of text using C#.net?
I need to print soft return to a text file/xml file. this text file will be generated using c#.net. you could verify if the answer is correct if you use NOTEPAD++ then enable the option to “View>Show Symbol > Show End of Line” then you will see a symbol like this:
Thanks in advance :)
Not sure what you mean by a soft return. A quick Google search says it's a non-stored line break typically due to word wrapping in which case you wouldn't actually put this in a string, it would only be relevant when the string was rendered for display.
To put a carriage return and/or line feed in the string you would use:
string s = "line one\r\nline two";
And for further reference, here are the other escape codes that you can use.
Link (MSDN Blogs)
In response to your edit
The LF that you see can be represented with \n in a string. Obviously you have a specific line ending sequence that you need to represent. If you were to use Environment.NewLine that is going to give you different results on different platforms.
var message = $"Tom{Convert.ToChar(10)}Harry";
Results in:
Tom
Harry
With just a line feed between.
Lke already mentioned you can use Enviroment.NewLine but I am not sure if that i what you want or if you are actually trying to append a ASCII 141 to your string as mentioned in the comments.
You can add ASCII chr sequences to your string like this.
var myString = new StringBuilder("Foo");
myString.Append((char)141);

Could not find Path

I am giving path to my file which i want to read in my program like this, path = "c:/users/abcd/desktop/read.txt" but while debugging i found it's showing like c://users//abcd//desktop//read.txt which is an invalid path.
Is there something i am missing or how should i go about declaring a path.
I would just use:
#"c:\user\abcd\desktop\read.txt"
(note the use of the #). Another likely issue is permissions; does the app have access to user acbd's desktop? You might also look at:
Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory)
and
Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
Try path = #"c:\user\abcd\desktop\read.txt"
I haven't worked with C# for a while, but is it possible that the debugger is just showing you the escaped version of the string but the string itself is ok? Click on the magnifying glass next to the string in your debugger to show the text visualizer and see if it looks ok there.
I'm guessing that these are backslashes. This is the debugger's way of showing certain characters. A newline would show up as "\r\n", a tab as "\t", and a backslash as "\\". The string "a\b\c\d" will show up in the debugger as "a\\b\\c\\d" because that is what you have to type into code to get the actual string "a\b\c\d". When the debugger shows doubled backslashes, each \ is actually a single .
The debugger is just showing you the escaped version of the file. I note that one of your paths contains the string "user" while the other contains "users" is the pluralization the problem?

Categories

Resources