This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 5 years ago.
Sometimes I find that many people use #string for locate a directory than use string even I though that they are same.
for example :
I have variable that called direct
string direct = "C:\\Users";
And then,I type :
System.IO.Directory.CreateDirectory(#direct);
I think it's same with
System.IO.Directory.CreateDirectory(direct);
But,what's difference between #direct and direct?
The # prefix allows you to use a reserved keyword as a variable name.
The following is an error:
string string = "C:\\Junk";
but the following is allowed (although a very bad idea):
string #string = "C:\\Junk";
The # prefix can also be used as a verbatim string literal:
string thefolderpath = #"C:\Junk";
If I had to guess, I would say the original programmer decided to use #string, #int etc. for temporary variables, as a style choice. A style choice, it must be said, akin to wearing socks with sandals.
Related
This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Convert C# string to JavaScript String
(3 answers)
Closed 4 years ago.
Salaam
I am looking for a proper version of a C# or Razor equivalent of PHP's addSlashes. That would add
\ to some\string => some\\string
Please provide help
Why I needed this
In my application a user entered Sometext in textbox was accidently pressed next time when page when data was populated though Razor it was like this
...append('<span>'+'#Model.value'+'</span>')
=> after compiling it becomes like this
...append('<span>'+'sometext\'+'</span>')
so with this scenario my javascript code broke at '\' because now single quote has started but not ending due to ``. So i thought instead of limiting characters i would rather add slashes through C# code
Thank You
You don't show any code you've already written, but this can be done by using [string.replace()] ( https://www.w3schools.com/jsref/jsref_replace.asp ) :
var str = "This is \\a test";
var replaced = str.replace("\\", "\\\\");
Whoops - you want the answer in C# , I misread your "javascript" tag. It's mostly the same:
string str = "This is \\a test";
string replaced = str.Replace("\\", "\\\\");
Also see C# String Replace
After the update, https://stackoverflow.com/a/27574931/34092 is most likely a much better answer.
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have this String
link="https%3a%2f%2fen.wikipedia.org%2fwiki%2fHuawei/"
which shoud be like this:
link="https://en.wikipedia.org/wiki/Huawei/"
I wrote this code:
link.Replace("%2f", "/");
link.Replace("%3a", ":");
But it did not work.
Instead of trying to decode the URL yourself I'd use HttpUtility.UrlDecode
HttpUtility.UrlDecode("https%3a%2f%2fen.wikipedia.org%2fwiki%2fHuawei/")
"https://en.wikipedia.org/wiki/Huawei/"
See: https://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode(v=vs.110).aspx
String.Replace does return the value replaced try:
link = link.Replace("%2f", "/");
link is a string and is not mutating when you call the Replace method
link.Replace will not affect the link object itself, instead it returns a new String
from the doc emphasis mine:
Returns a new string in which all occurrences of a specified Unicode
character in this instance are replaced with another specified Unicode
character.
do instead:
link = link.Replace("%2f", "/"); or
link = link.Replace("%3a", ":");
This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 5 years ago.
In this scenario the data I could have in my string may look like below but keep in mind the ids are dynamically generated so this isn't static and could be more than 2 if you haven't caught onto that.
ing:server blah blah, you. 2019,;:10-!gs.csd
1. id=value, otherid=value, pos=(22,22,33)
2. id=value2, otherid=value2, pos=(2g,2g,f) info other info info info info etc etc.
EDIT: How am I supposed to extract the individual values into strings afterwards from the string, the following does not work:
String valueString = "csd 1. id=value, otherid=value, pos=(22,22,33) ";
String value = valueString.Substring(valueString.IndexOf("otherid"), valueString.IndexOf(",") - valueString.IndexOf("otherid"));
You can do with Substring since you have already way of expecting when to start and when to end on your searching.
string result = x.Substring(x.IndexOf("csd"), (x.IndexOf("info ") - x.IndexOf("csd")));
I start searching on the start of the word "csd" and ends with the word "info " (with space), since there is also a word of info at the beginning of your string.
The result would be:
"csd 1. id=value, otherid=value, pos=(22,22,33) 2. id=value2, otherid=value2, pos=(24,21,33) "
This question already has answers here:
Use the long reserved word as a variable name in C#
(5 answers)
Closed 6 years ago.
So I have this simple problem that I'm struggling with. Consider this code:
namespace Foo
{
public class Bar
{
public void Test(string object)
{
}
}
}
This function throws a syntax error because object is a keyword in C#. Is there a way to solve that? In my real code I have a framework that uses function's signature to create an API and I should really use object name as parameter.
Use # before parameter name #object to use keyword as identifier
public void Test(string #object)
From C# Language Specification 2.4.2 Identifiers:
The rules for identifiers given in this section correspond exactly to those recommended by the Unicode Standard Annex
31, except that underscore is allowed as an initial character (as is
traditional in the C programming language), Unicode escape sequences
are permitted in identifiers, and the “#” character is allowed as a
prefix to enable keywords to be used as identifiers.
The # symbol allows you to use reserved word. For example:
int #class = 15;
The above works, when the below wouldn't:
int class = 15;
This question already has answers here:
Can I expand a string that contains C# literal expressions at runtime
(5 answers)
Closed 6 years ago.
I have a literal as a string in a file
def s_CalculatePartiallyUsedTechPenalty(rate):\n total = min(rate,0)\n title = \"Partially Used Technology Penalty\" \n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)
when reading the file the text obviously looks like this:
def s_CalculatePartiallyUsedTechPenalty(rate):\\n total = min(rate,0)\\n title = \\\"Partially Used Technology Penalty\\\" \\n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)
Is there clean way to convert this string so that the value in the file is also the actual value of the string in code. In other words that that \n for example is \n and not \\n.
I understand that I can write a method that goes and replaces all the applicable values, but I do not want to do that unless it is the only way.
Edit: In response to John Wu's answer. No I am not confused. I do understand exactly that this is happening however I want to convert the literal value "\n" to the newline character. So instead of the literal value of \n it should be a new line.
Basically the inverse of How to convert a string containing escape characters to a string
You are confusing yourself. The string held in the file will be exactly the same as the string held in a string variable obtained by reading the file.
Perhaps you are using Visual Studio to inspect the string (i.e. using the Watch window or just hovering over the variable while the code is in debug mode). In this case, Visual Studio will display the extra slash to indicate that the string variable contains the literal value "\n" instead of a newline character.
If you want to eliminate the escape characters in the Watch window, you can append the format specifier ,nq to the variable name (link).
See also this question on StackOverflow.
If you can not fix file-writing code, that you can solve this issues in a following way:
String.Replace(#"\\\", #"\");
String.Replace(#"\\", #"\");
Or, in case, if you normal unescaped string,
String.Replace(#"\\\""", "\"");
String.Replace(#"\\n", Environment.NewLine);
P.s. Also think about other special characters, like \t
UPDATED:
Even better approach:
class Program
{
static void Main(string[] args)
{
var escaped = #"def s_CalculatePartiallyUsedTechPenalty(rate):\n total = min(rate,0)\n title = \""Partially Used Technology Penalty\"" \n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)";
var unescaped = Regex.Unescape(escaped);
Console.WriteLine(unescaped);
}
}