Transform String Folder Path [duplicate] - c#

This question already has answers here:
Full path with double backslash (C#)
(5 answers)
Closed 9 years ago.
I'm doing a C# project for school.
I need to extract a .zip file but i have a problem.
I get the path where the file that is going to be extract is with FolderBrowserDialog and everything is ok, but the FolderBrowserDialog gives me something like "C:\Users\Zé Eduardo\Music" , but i need something like this "C:\\Users\\Zé Eduardo\\Music".
How can i transform "\" to "\\"?

well, this is the answer to your question but you are probably asking the wrong question,
var transformedString = badString.Replace(#"\", #"\\");
The # in the literal means, this is a verbatim string so normal escaping rules don't apply. Effectively, you don't need to escape the escape character.

Something simple would be to use string replace:
String original = #"c:\some\path";
String #fixed = original.Replace("\\", "\\\\"); //Note the double escaping!
//fixed contains "c:\\some\\path"

Related

I want to do a search in real time of objects with Regex in c# [duplicate]

This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.

C#: String with escaped quote ends up with three escaped characters [duplicate]

This question already has answers here:
Replace "\\" with "\" in a string in C#
(9 answers)
Escape Characters Aren't Working In Debugger (C#)
(2 answers)
How can I add \ symbol to the end of string in C#
(8 answers)
Why does .NET add an additional slash to the already existent slashes in a path?
(4 answers)
Escape sequence with "\" symbol
(4 answers)
Closed 2 years ago.
I'm struggling with escape characters in C#.
I'm saving a varchar field in a SQL Server database with a quote that needs to be escaped. So, an example could be something like that: (\"value\")
When my frontend application consume this value from API I get the value with a single escape character as i have in the DB.
The problem come up when consuming this API from C# code. For some reason this single character (\) is replaced for three of them (\\\).
After debugging the code I found that when I retrieve the data I get this:
debugging the value
When I click to check the value it seems OK:
Value into Text Visualizer
My question is why theese two extra backslashes are added and how can I get rid of them in order to get only one => (\").
Hope anyone can help me :)
Thanks!
You value should be correct. The debugger window escape the “/“, therefore it appears like escape(backslash)escape(quote).
(//)(/“)

How to add "" in to string.Format in C# [duplicate]

This question already has answers here:
How can I add double quotes to a string that is inside a variable?
(20 answers)
Escape double quotes in a string
(9 answers)
String.Format store double quotes inside string [duplicate]
(4 answers)
Closed 2 years ago.
I need to pass the folder path to the Python interface but spaces in the path make it complicated.
Example :
Defect.py C:\TestImage\Images works fine.
However, if there is a space in the path as below,
`Defect.py C:\**Test Image**\Images`
causes an error due to two separate parameters.
If I could pass it as :
Defect.py "C:\**Test Image**\Images" should work
So I am currently trying to use string.Format("{0}") to format this string with double quotations but does not work.
Please let me know how I could format the statement using string.Format. I need to use string.Format since there are multiple other parameters.
Try this:
string.Format("Defect.py \"C:\\{0}\\Images\"", "Test Image");
Bear in mind that the whole trick is to escape the character using the \" backslash escape character. The rest of it was just to show how to use the string.Format

Is it possible to place a string variable inside a Regex? [duplicate]

This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.

How to properly escape \ character in c#? [duplicate]

This question already has an answer here:
Backslash Escape Sequence displaying both backslashes
(1 answer)
Closed 7 years ago.
I have a string like,
var log = "C:\\folder\\folder2\\this.txt";
For some reason this does not escaped the backslash, it still shows the backslashes in the string as below:
C:\\folder\\folder2\\this.txt
How do I properly escaped the \ character to allow writing to a file at such a location?
You are looking at it in the debugger, There is no double back slash in actual string. The debugger shows it like that.
Do:
Console.WriteLine(log);
and it will display the string without double back slashes in the console.
You are bound to see double backslashes if you're in the IDE, but they won't be there when you actually use the string.
As a bonus, you can rewrite your string as var log = #"C:\folder\folder2\this.txt";

Categories