I have a issue when I look up a user name using the code below if the user has \v in their name. For Example, if their user name is xxx\vtest, it can't find the backslash when using the LastIndexOf function.
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\\") + 1);
If I do try this, it works with no problems.
string strUser = #"xxx\vtest";
strUser = strUser.Substring(strUser.LastIndexOf("\\") + 1);
Any ideas on how to ignore the escape character while using User.Identity.Name?
If the name has 'backslash v' you should find a backslash, if the name has a vertical tab (escaped '\v') you will not find a backslash. The reason you find a backslash in the second version is because you used an # with the string declaration more info.
try this:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\v") + 1);
EDITED
If you know which escape characters you are looking for you can also use this:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOfAny(new char[]{'\b','\t','\n','\v','\f','\r','\\'}) + 1);
If you know that there is exactly one character that follows the domain you can also use Regex to get the user.
string strUser = this.Context.User.Identity.Name;
strUser = Regex.Replace(strUser, #"^(xxx).(.+)$", "$2");
// or
strUser = Regex.Replace(strUser, #"^([a-zA-Z]*?)[^a-zA-Z](.+)$", "$2");
// or
strUser = Regex.Replace(strUser, #"^(.*?)[\t\n\v\f\r\\/<> ,.#](.+)$", "$2");
// it all depends on your input
Related
I have providing one field on c# and run it on sql, the problem is on string that I post to sql, I have double quotation, and it goes to sql with \", how could I solve it
string url = #" ''+Geo.Name+'';
//note: url can be like this and isn't matter on result
//string url = " ''+Geo.Name+'';
string filterExpression=string.Format("SELECT Geo.Count as c,{0} as url from Geo ", url);
IQueryable<GeoDto> geoResult = _entities.Database.SqlQuery<GeoDto>(filterExpression).AsQueryable();
return geoResult ;
But on result I see \", I looking for only double quotation result(without \)
href attribute can use single quotation.
You could try
string url = "'<a href=''/p?geoId='+Geo.GeoId+'''>'+Geo.Name+'</a>'";
Can you try:
string url = " ''+Geo.Name+'' ";
Without the # escape.
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);
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
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 \".