I need to add a '\' to a string
I try this
var Filename = name.Replace("'", "\'");
name = Filename ;
if name = he's here
name.Replace("'", "\'") will return : "he\\'s here"
what I need is: he\'s here
First name.Replace("'", "\'") does nothing because "'" == "\'". So name.Replace("'", "\'") returns "he's here" (you can try it in https://dotnetfiddle.net/). What you want is: name.Replace("'", "\\'")
Second if you inspect name in the debugger (in watch window or immediate window) you will get "he\\'s here" because that is how you should write a string constant in c# to get he\'s here into a variable.
Related
I need to replace the string "AAAA:123346hadhdhajkkd890" with "AAAA":"123346hadhdhajkkd890" using the Replace function in C#.
Hi try to use the code below :
var str = "AAAA:123346hadhdhajkkd890";
str = str.Replace(":", "\":\"");
in order to put " in Replace function you have to use \" in the parameter.
var s1 = "AAAA:123346hadhdhajkkd890";
var s2 = str.Replace(":", "\":\"");
This is because \" will actually tell the compiler " and if simply " will mean the end of the string. This is known as the escape sequence.
I have the following code:
if (BrowserName.ToUpper().Contains("FIREFOX"))
privateModeParam = " -private-window";
string extraspeech = "\"";
string both = BrowserName + extraspeech + privateModeParam;
Process.Start(both, URLFromDB);
When it run's it returns the following value:
BrowserName = "c:\\program files\\mozilla firefox\\firefox.exe"
both = "c:\\program files\\mozilla firefox\\firefox.exe\" -private-window"
privateModeParam = " -private-window"
What I need to do is, trim the \ from both string because it won't open firefox with that back slash.
I should add if I simply do:
string both = BrowserName + privateModeParam;
the value returned is "c:\program files\mozilla firefox\firefox.exe\ -private-window"
what won't open Firefox
What causes your problems is the double-quote ("), not the back-slash. There is no backslash at this position in the string, it's only displayed like that by your debugger because c# uses \ to escape things like " inside string literals.
So your problem seems to be that you forgot to add extraspeech before the executable, too:
string both = extraspeech + BrowserName + extraspeech + privateModeParam;
or better
string both = $"\"{BrowserName}\" {privateModeParam}"; // C#6
string both = string.Format("\"{0}\" {1}", BrowserName, privateModeParam); // pre C#6
Update:
But the real problem here seems to be that you pass one command line argument in the fileName parameter and one via the arguments parameter.
You should actually call Process.Start like that:
Process.Start(BrowserName, $"{privateModeParam} {URLFromDb}");
Simply pass all arguments in side the arguments parameter. Then there is also no need to wrap the executable in double-quotes as it is the only string in the fileName argument. See documentation for more information about the parameters to Process.Start().
The easiest way would be to use Substring:
MyString = MyString.Substring(0, MyString.Length - 1);
if (BrowserName.EndsWith("\\")){
BrowserName = BrowserName.Substring(0, BrowserName.Length - 1);
}
or
both = both.Replace("\\\"", "");
may fix your problem
I need to pass a URL from C# to javascript. The problem is if the filename has single quote, it does not execute the javascript. When I use HttpUtility.HtmlEncode(fileNameWithoutEx) it does execute javascript but if filename is say "Copy of David's Birth Certificate" then URL gets converted to ?View.aspx?length=60&ext=pdf&file=Copy of David' Birth Certificate.
When View.aspx tries to get the querystring file i.e; filename, it gets set to "Copy of David" and not "Copy of David's Birth Certificate". Because of & it does not get the rest of the querystring.
if (System.IO.File.Exists(fileLocation)) {
string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(fileLocation);
string fileExtension = System.IO.Path.GetExtension(fileLocation).Replace(".", "");
string title = System.IO.Path.GetFileNameWithoutExtension(fileName);
string url = "View?length=" + 60+ "&ext=" + fileExtension + "&file=" + fileNameWithoutExt;
ScriptManager.RegisterStartupScript(this, GetType(), "ShowPDF", "$(document).ready(function(){ShowPDFDocument('" + title + "', '" + url + "');});", true);
}
How can I send url with single quote to javascript?
What's the best way to handle ' and other special characters?
Use Uri.EscapeDataString(yourLinkHere);. See Uri.EscapeDataString on MSDN.
You're embedding the title and URL within quote-delimited strings in JavaScript, so you need to escape them.
title = title.Replace("'","\\'");
url = url.Replace("'","\\'");
You can try percent codes. I would recommned using %20 in the place of spaces as well. You can chance the file name itself or the way you route to it in code.
urlstring.replace("'", "%27")
Try to replace your ' with %27 which is the standard percent encode for '
Characters with reserved purposes should be replaced to guarantee functionality in all environments. You can view the list and read up on this here.
I have an example username:
corp\myusername
So if I put this in C# it wants to escaoe it with "\" so it turns out to be:
corp\\myusername but I need to to be corp\myusername. Any ideas how to get it to work?
Thanks much
using verbatim string
string username = #"corp\\myusername"
or
"\\" = \ so "\\\\" = \\
string username = "corp\\\\myusername"
try adding a "#" symbol in front of the string like this:
String.Format(#"corp\myusername")
or double up the "\" to escape the escape :)
Use a verbatim string literal.
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
Precede the literal with "#" character and you can then type that.
I imagine you're trying to separate the domain from the user name, to get the username for your application when using Windows authentication in your site, yes? I also ran into the issue where some machines would return the value as upper case, and some as lower case, so be careful to force the case in your source code (or database)!
string windowsLogin = HttpContext.Current.User.Identity.Name;
int hasDomain = windowsLogin.IndexOf(#"\");
if (hasDomain > 0)
{
windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
}
return windowsLogin.ToLower();
I am trying to use a String.Format to create the following string
2MSFX.exe "C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\Clear.fx" "C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\Clear.mxfb"
so i am trying to use String.Format, but i just can't seen to get my head around it for some reason :|
The code is (where last 2 params are String.Empty):
String outputFile = Path.Combine(destDir, Path.ChangeExtension(Path.GetFileName(fxFile), "mgxf"));
String command = String.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", Path.GetFullPath(fxFile), Path.GetFullPath(outputFile), DX11Support, DebugSupport);
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = MGFXApp,
Arguments = command,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
But that appears to be giving me
\"C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\ClearGBuffer.fx\" \"C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\MGFX\ClearGBuffer.mgxf\" \"\" \"\"
If i use the verbatim string i can't get it to create the string i want.
Any Ideas?
Thanks.
Update
You should use String.Concat().
String.Concat("\"", Path.GetFullPath(fxFile), "\" " , Path.GetFullPath(outputFile), "\" " DX11Support,"\" " ,DebugSupport, "\"")
For a simple case like this, I wouldn't think it necessary, but you could create an extension method to automatically put quotes around the strings.
public static class StringExtensions
{
public static string Quotify(this string s)
{
return string.Format("\"{0}\"", s);
}
}
Then your command format looks like this:
String command = String.Join(" ",
Path.GetFullPath(fxFile).Quotify(),
Path.GetFullPath(outputFile).Quotify(),
DX11Support.Quotify(), DebugSupport.Quotify());
You need to use a combination of the # literal to avoid '\' squirlyness, and ""'s to make "'s
This example works for me:
string s = #"""C:\Test Dir\file.fx"" ""C:\Test Dir\SubDir\input.dat""";
Console.WriteLine(s);
Console output looks like this:
"C:\Test Dir\file.fx" "C:\Test Dir\SubDir\input.dat"
Just remember that two quotes makes a single quote, so the triple quote at the beginning and end of the string are the quote to start the string definition, and then a double quote to make a quote. Possibly one of the more confusing string formats out there, but that's how it works.