I am new to c sharp
can anybody say what the mistake
string cPict= "Picture\"+firstSelectedItem+".jpg";
where
"Picture\" = folder
firstSelectedItem = Employee Number
".jpg" = file extension
getting following error
string does not contain definition for jpg
please help
thanks in advance
The problem is that in "\"+firstSelectedItem all is treated as string, even the firstSelectedItem variable because you've used the \-character to escape the following ".
You either have to
escape the \-character by another one,
use a verbatim string literal or
use the Path-class, especially Path.Combine:
1)
string cPict = "Picture\\" + firstSelectedItem + ".jpg";
2)
string cPict = #"Picture\" + firstSelectedItem + ".jpg";
3)
string cPict = Path.Combine("Picture", firstSelectedItem + ".jpg");
You can replace it with normal slash like that:
string cPict= "Picture/"+firstSelectedItem+".jpg";
The \ is a special character that escapes the next character in a string, therefore, according to the compiler then + firstSelectedItem + is still part of the string. Your code should look like one of the following:
string cPict = #"Picture\" +
or:
string cPict = "Picture\\" +
and that should work.
you need to escape the backslash \ character
string cPict= "Picture\\"+firstSelectedItem+".jpg";
learn about Escape Sequences here
The solution is to add double slashes as below:
string cPict= "Picture\\"+firstSelectedItem+".jpg";
"Picture\\"=folder
Related
My code here:
String text = "" + label1.Text + ""; //label1 is: C:\myfiles\download
textBox1.Text = text;
I would textbox shown after I built: (has quotes)
"C:\myfiles\download"
Please help me.
Thank you very much.
Sorry my English is bad.
In order to avoid such errors, use formatting:
textBox1.Text = String.Format("\"{0}\"", label1.Text);
and let compiler ensure that the provided string "\"{0}\"" is free from typos.
This?
String text = "\"" + label1.Text + "\""; //label1 is: C:\myfiles\download
This escapes the quotes meaning: the character after the \ has no special meaning any more and is a usual character.
Or even easier using verbatim-string:
String text = #"""" + label1.Text + #""""; //label1 is: C:\myfiles\download
Using C# 6, you can very neatly do this using the following syntax:
textBox1.Text = $"\"{label1.Text}\"";
This is shorthand for textBox1.Text = String.Format("\"{0}\"", label1.Text); and, as with String.Format, the compiler will check the validity of the string for you.
Solving your problem
You need to use backslashes. The second occurence of " will escape the string declaration. Backslashes will prevent this.
String text = "\"" + label1.Text + "\"";
textBox1.Text = text;
Now, a little refactoring
You don't need to declare a variable, except you use this value again. Futhermore, you can use string.Format(). For more information about this method, watch the link in the references section.
textBox1.Text = string.Format("\"{0}\"", label1.Text);
Some references
This Stackoverflow-question focuses on the same problem:
How to use a string with quotation marks inside it?
Here a MSDN reference: https://msdn.microsoft.com/en-us/library/aa983682(v=vs.71).aspx
MSDN reference string.Format
Double quotation mark represented by \" as an escape sequence character like;
String text = "\"" + label1.Text + \""";
Or you can double it with a verbatim string literal as;
String text = #"""" + label1.Text + #"""";
Or you can use string.Format as;
String text = string.Format("\"{0}\"", label1.Text);
How to make a variable in a path in c#? For like creating a user register/login/stats.
string UserName = "";
string path = #"c:\File\File\" + UserName + ".text";
I know this doesn't work, maybe does anybody know how to do it else, I search around and never found a solution to get a path like this.
I hope somebody will solve it!
You can use / (slashes) instead of \ (backslashes) or you can escape the backslash adding another backslash behind it:
string path = "c:\\File\\File\\"+ Username + ".text";
That way is absolutly OK for simple concating strings. There are other ways like
string.Format function
or the
StringBuilder class
This is all absolutly OK but if you will be absolutly sure that you create a vaild Path use
Path.Combine
The only reason that doesn't work is because of the escape characters. Any of the following will work;
string path = "c:\\File\\File\\"+ Username + ".txt"; // escape first slash, second appears in string
string path = #"c:\File\File\"+ Username + ".text"; // take literal string, escape sequences included
string path = "c:/File/File/"+ Username + ".text"; //forward slash is not an escape
You can easily get an array of invalid file name characters
char[] invalidPathChars = Path.GetInvalidPathChars();
foreach (char ch in invalidPathChars)
{
Username = Username.Replace(ch.ToString(), "");
}
How can I properly use substring, at least, make it work ?
I want to find the position of this substring '#fff'\"> in the stringString this.style.backgroundColor='#fff'\">Choose a translation mode,
I'm doing this :
string substrToSearch = "'#fff'\\\">";
int substrPosition = stringString.IndexOf(substrToSearch);
Unfortunately, substrPosition = -1, and it is not working.
May someone help me ?
EDIT SOLVED :
I don't realy understand why I can't do this string quote = " " "; but I can do this quote = " \" " ,in fact I know that I can but.. here, the "\" is used as an escaping character, so why in the substring that I'm searching it is not interpreted as an escape character but as simple text ?
To me , string substrToSearch = "'#fff'\">";, substrToSearch is '#fff'"> as \" = ".
To be simple, why \" is not interpreted as an escape character ?
try this:
string substrToSearch = "'#fff'\">";
the stringString is not ecapsed when you access it
Here you go...
[TestMethod]
public void StringIndex
{
var stringString = "this.style.backgroundColor='#fff'\">Choose a translation mode,:";
var substrToSearch = "'#fff'\">";
var substrPosition = stringString.IndexOf(substrToSearch);
}
The answer to your added question is fundamental to the concept of string escaping. The syntax string quote = " " "; is problematic because a compiler would think that the string ended with the second quote and not the third one. String escaping allows representing an actual quotation mark with \", so the actual value of " \" " is ", not \".
I have already seen the other way around. But this one I can not catch. I am trying to get a part of a web resourcePath and combine it with a local path.
Let me Explain a bit more.
public string GetLocalPath(string URI, string webResourcePath, string folderWatchPath) // get the folderwatcher path to work in the local folder
{
string changedPath = webResourcePath.Replace(URI, "");
string localPathTemp = folderWatchPath + changedPath;
string localPath = localPathTemp.Replace(#"/",#"\");
return localPath;
}
But, When I do this the result is like
C:\\Users
But what I want to have is
C:\Users
Not "\\" but my debug shows it like C:\\Users but in the console it shows it as I expect it.
I want to know the reason for that
thanks..
Because \\ is escape sequence for \
string str = "C:\\Users";
is same as
string str = #"C:\Users";
Later one is known as Verbatim string literal.
For combining paths in code it is better to use Path.Combine instead of manually adding "/"
Your code should be like
public string GetLocalPath(string URI, string webResourcePath,
string folderWatchPath)
{
return Path.Combine(folderWatchPath, webResourcePath.Replace(URI, ""));
}
There is no need to replace / with \ because path names in windows supports both. So C:\Users is same as C:/Users
In C#, \ is special in ""-delimited strings. In order to get a literal \ in a string, you double it. \ is not special in #"" strings, so #"\" and "\\", or #"C:\Users" and "C:\\Users" mean exactly the same thing. The debugger apparently uses the second style in your case.
I believe that debug shows strings with escape chars, and to escape a \ in a non-verbatim (not prefixed with #) string you have to write \\.
I have a string object in c# with a bunch of elements delimited by '/' characters. The string will look something like this:
"element1/element2/element3/element4"
What's the most efficient way to change the last element in the '/' delimited string?
Use string.LastIndexOf:
string s = "element1/element2/element3/element4";
s = s.Substring(0, s.LastIndexOf('/') + 1) + "foo";
If this is a filename/path string, you should use the System.IO.Path for this.
is there a 'lastIndexOf' in the C# String class? ( I don't code in C# normally ), if it exists you could use that to get a reference to the last / in the string, and that / precedes the last element of your string.
Like Joel suggests.. maybe something like this:
string path = (System.IO.Path.GetDirectoryName(#"element1/element2/element3/element4") +
System.IO.Path.DirectorySeparatorChar + "foo");
string new_path = path.Replace(System.IO.Path.DirectorySeparatorChar, '/');