Replace substring not working [duplicate] - asp.net

This question already has an answer here:
C# how to replace Slash Quote \"
(1 answer)
Closed 8 years ago.
I am working in asp.net mvc. want to replace \ character to / character. But it is not working.
Let
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\","/");
I get error in second line. Please help how to replace.

Try this:
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\\","/");
You need to escape the backslash in the first argument for it to be treated as...a backslash (i.e. "\\" instead of "\").

You need to escape back-slashes. The easiest way is to prefix your string with #:
path=path.Replace(#"\","/");
Another method is to escape it with another backslash:
path=path.Replace("\\","/");

try this
th=path.Replace("\\","/")

\ is a special escape character in string literals in c#.
You can precede the string with # to make it verbatim or escape the \ with another \:
path=path.Replace(#"\","/");
or
path=path.Replace("\\","/");

\ is escape character, so your code will not even compile use # or \\ to make the code compile. then it will work
string path=#"D:\Qamar\Cartoons\Image.jpg";
path=path.Replace(#"\","/");
or
string path="D:\\Qamar\\Cartoons\\Image.jpg";
path=path.Replace("\\","/");
but if you are working with Path or URI you can use the inbuilt C# methods to do it like below
System.Uri uri1 = new Uri(#"D:\Qamar\Cartoons\Image.jpg");
string whatYouWant = uri1.AbsolutePath; //Result is: "D:/Qamar/Cartoons/Image.jpg"

Related

How to pass exact string as parameter in robot framework

I have the follow test case :
Some function
    Given some condition
    When I go to "\\path\to\folder"
    Then I don't know
I want to use exact: **"\\\\path\to\folder"** as string in my keyword argument
When I use above string, I'm getting escaped value.
I tried to use:
r"\\path\to\folder"
'\\path\to\folder'
Thank you.
The backslash is an escape character in robot, so to send a backslash you must escape the backslash.
When I go to \\\\path\\to\\folder
One solution is to escape it once again:
When I go to \\\\path\\to\\folder
Or you can use:
When I go to ${/}${/}path${/}to${/}folder
which will pass \\path\to\folder.
Another way to do it is to use a python raw string and just Evaluate it:
${my_string} Evaluate r"a\\b"
Log To Console ${my_string}

Warning on regex string in Python

So, I am doing a small function to strip all the weird chars from a string, eg. #$& will be replaced just for a " "
The chars I am trying to remove are the following, defined into a string:
xChars = r"#$%()'^*\;:/|+_.–°ªº"
However I kepp getting the warning:
Anomalous backslash in string: '\;'. String constant might be missing an r prefix
However, when i used the r prefix eg. r"\" python rules out some of the special chars i want to replace. It doesnt produce an error it just thinks that those chars are ok or something and it rules them out.
Any ideas on how to fix this ?
Normally backslashes escape characters, therefore the compiler isn´t sure if the backslash has to be escaped. Maybe try using a double backslash to escape the backslash itself like: xChars = r"#$%()'^*\\;:/|+_.–°ªº"

How to replace '/' with '\\' using QString replace()?

Can anyone help with following? Suppose I have a QString with a filepath stored of a certain file, I want to replace the /(slashes) from it with \\(double backslashes) I tried:
mystring.replace("/","\\");
But it only puts a single \ instead of \\
String before replacement: D:/myfiles/abc.zip
String after replacement: D:\myfiles\abc.zip
Expected string: D:\\myfiles\\abc.zip
You need to use:
mystring.replace("/","\\\\");
The compiler uses \ as an escape character in strings (for things like \t, \n or \r) so that \\ is actually turned into \. If you need two backslashes, you need to start with four.
If you want to convert paths to Windows format, you could simply use QDir::toNativeSeparators():
qDebug() << QDir::toNativeSeparators("c:/windows/path"); // Prints "c:\windows\path"
1) Why do you want to replace them, AFAIR forward slashes work in file operations too (irrespective of OS).
2) Did you try to read the docs - for example why \\ results in single backslash?

ConfigurationManager.AppSettings convert "\n" to "\\n" why?

I have a AppSetting in web.config.
<add key="key" value="\n|\r"/>
When i read it by ConfigurationManager.AppSettings["key"] it gives "\\n|\\r".
Why ?
In the debugger, becuase the backslash is a special character used for things like tabs (\t) and line endings (\n), it has to be escaped by the use of another backslash. Hence any text that contains an actual \ will be displayed as \. If you print it out to a file or use it in any other way, you will find your string only contains the one .
This isn't ConfigurationManager doing anything.
The backslash escaping syntax is only recognized inside of string literals by the C# compiler. Since your string is being read from an XML file at runtime, you need to use XML-compatible escaping (character entities) in order include those characters in your string. Thus, your app settings entry should look like the following:
<add key="key" value="&x10;|&x13;"/>
Because 10 and 13 are the hex values for linefeed and carriage return, respectively.
Like cjk said, the extra slash is being inserted by the debugger to indicate that it is seeing a literal slash and not an escape sequence.
I solved the same problem with a string replacement.
Not beautful.. but works!
ConfigurationManager.AppSettings["Key"].Replace("\\n", "\n")
string str = "\n";// means \n
string str1 = #"\n";// means \\n
From the AppSettings, It seems that when you extract the key's value, # is internally wrapped.. It is done by the compiler not runtime.

asp.net mvc file path issue \

i am using this to create a new folder
System.IO.Directory.CreateDirectory(#" + somevariable);
the thing is that when i enter the folder c:\newfolder\newfolder in the textbox and is trying to recieve the value up in the controller it is replaced with double slash( \) c:\\newfolder\\newfolder. how would i prevent \ quotes from coming in the path
Secondly the string.replace is also not working for replacing \ with \\
string strText = OrganMeta.vcr_MetaValue;
string gf = strText.Replace("\\", #"\");
"\\" is equivalent to a string of one character, a backslash.
#"\" is also equivalent to a single character, a backslash.
so your Replace method is replacing one form of a backslash with a different form.
try this:
string gf = strText.Replace( #"\\", #"\" );
OR
string gf = strText.Replace( "\\\\", "\\" );
as far as the folder thing goes, Andy is right, it will show a double-backslash in the IDE when in fact there is only one in the string. is there an error when Directory.CreateDirectory() is called? or is the folder created?
Are you sure it's replaced it with \\? If you hover over the variable it will appear to have \\ where there should be a single \ but if you view it in the text visualizer it will show correctly.
Not sure what you mean by string.replace is not working...?? Can you give an example of the code that's not working?
Slashes don't get doubled between the form submit and your controller action.
It's far more likely that you're viewing the result in the debugger or another context that shows two slashes to allow you to distinguish between escaped characters (\n) and a literal slash ().
Write the string to the debug window to verify this.
System.Diagnostics.Debug.WriteLine("SomeText");

Resources