What does it mean when I enclose a C# string in #" "? [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does # mean at the start of a string in C#?
Sorry but I can't find this on Google. I guess it maybe is not accepting my search string when I do a search.
Can someone tell me what this means in C#
var a = #"abc";
what's the meaning of the #?

It is a string literal. Which basically means it will take any character except ", including new lines. To write out a ", use "".

The advantage of #-quoting is that escape sequences are not processed,
which makes it easy to write, for example, a fully qualified file
name:
#"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

It means it's a literal string.
Without it, any string containing a \ will consider the next character a special character, such as \n for new line. With a # in front, it will treat the \ literally.
In the example you've given, there is no difference in the output.

This says that the characters inside the double quotation marks should be interpreted exactly as they are.
You can see that the backslash is treated as a character and not an
escape sequence when the # is used. The C# compiler also allows you to
use real newlines in verbatim literals. You must encode quotation
marks with double quotes.
string fileLocation = "C:\\CSharpProjects";
string fileLocation = #"C:\CSharpProjects";
Look at here for examples.

C# supports two forms of string literals: regular string literals and verbatim string literals.
A regular string literal consists of zero or more characters enclosed
in double quotes, as in "hello", and may include both simple escape
sequences (such as \t for the tab character) and hexadecimal and
Unicode escape sequences.
A verbatim string literal consists of an # character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is "hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences and hexadecimal and Unicode
escape sequences are not processed in verbatim string literals. A
verbatim string literal may span multiple lines.
Code Example
string a = "hello, world"; // hello, world
string b = #"hello, world"; // hello, world
string c = "hello \t world"; // hello world
string d = #"hello \t world"; // hello \t world
string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me
string f = #"Joe said ""Hello"" to me"; // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
string h = #"\\server\share\file.txt"; // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = #"one
two
three";
Reference link: MSDN

Related

Convert special character in string into a char

I have this string:
string specialCharacterString = #"\n";
where "\n" is the new line special character.
Is it possible convert/assign that string (of two characters) into a (single) char. How do I do something like:
char specialCharacter = Parse(specialCharacterString);
Where specialCharacter value would be equal to \n
Is there anything in dotnet that would parse the string for me or must I use if or switch the string (the string can contain any special character) to accomplish what I want. Note that char.Parse(string) cannot handle special characters and thinks the string above is actually two characters.
Maybe I am oversimplifying but can't you just do the following:
txtString.Replace("\n", "$");
It is technically a string to string replacement but would be string to char...
You can always cast it to a char since you know what char you are replacing the string with.
Not sure, what business need it is, but if you need parsing C# in C# you can use some tools like Antlr, which supports C# grammar (https://github.com/antlr/grammars-v4/)
I don't think there is any ready tool designed just for strings
Try use Regex.Unescape(specialCharacterString);
It will return the new string with escape characters.
For example:
var literalStringWithEscapeCharacters = #"Hello\tWorld";
var stringWithEscapeCharacters = Regex.Unescape(literalStringWithEscapeCharacters);
Console.WriteLine(stringWithEscapeCharacters);
Will print: Hello World
Instead of: Hello\tWorld
Then you can find escape characters in stringWithEscapeCharacters like this:
var escapeChars= new [] { '\n' };
var characters = stringWithEscapeCharacters.Where(c => escapeChars.Contains(c)).ToList();
All escape characters described here:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#string-escape-sequences

Regex to validate String like C# does

I have this pattern to validate that if a string is correct or not.
public string isStringConstant(string vPart)
{
string pattern = #"^\""[\\(t|r|n|0|b|f|""|a|v|')a-zA-Z0-9,\.\*/;\~!\{\}##\$%\^:&\(\)\+\[\]<>_\?=\-`]*\""$";
Regex obj = new Regex(pattern);
if (obj.IsMatch(vPart))
{
return "StringConstant";
}
return "INVALID";
}
It works well but it also validates the following string which it should not.
"harisnabeel\"
The input string source is a text file.
What wrong I am doing with that pattern?
Look at this part: \\(t|r|n|0|b|f|""|a|v|')
You are trying to shortcut having to write out \t\r\n, etc. In the process you have \, which says \ is a valid character in your string. Rewrite your "or this or that" portion long hand and you should be fine. Don't have time to test this personally, but a bit of experimentation with that will solve your problem.
If we follow the title (Regex to validate String like C# does), the full regex should be:
// https://learn.microsoft.com/it-it/dotnet/csharp/language-reference/language-specification/lexical-structure#string-literals
// A character that follows a backslash character (\) in
// a regular_string_literal_character must be one of the following
// characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v.
// Otherwise, a compile-time error occurs.
var rx = new Regex(#"^""(\\(?:['""\\0abfnrtv]|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}|x[0-9A-Fa-f]{1,4})|[^\\""]+)*""$");
All the "" are double, because the regex is based on a string-literal (#"..."), so they became a single " each.
Note the ending [^\\""] that becomes [^\\"] because the string is a literal string, so any character that isn't an escape \ or a " (the \ is handled separately, the unescaped " is the ending of the string). Note even the doubling of the \\, because otherwise a single \ becomes an escape sequence of the regex, and the handling of \x, \u and \U.
Example of use:
var res = rx.Match(#"""Hello\nWorld\\\""\x123G""");
The "pieces" of the string:
var pieces = res.Groups[1].Captures.Cast<Capture>().ToArray();
In this case Hello, \n, World, \x123, G

Replacing doubleslash to single slash

In my c# application i want to convert a string characters to special characters.
My input string is "G\u00f6teborg" and i want the output as Göteborg.
I am using below code,
string name = "G\\u00f6teborg";
StringBuilder sb = new StringBuilder(name);
sb = sb.Replace(#"\\",#"\");
string name1 = System.Web.HttpUtility.HtmlDecode(sb.ToString());
Console.WriteLine(name1);
In the above code the double slash remains the same , it is not replacing to single slash, so after decoding i am getting the output as G\u00f6teborg .
Please help to find a solution for this.
Thanks in advance.
string name = "G\\u00f6teborg";
Just remove one of the backslashes:
string name = "G\u00f6teborg";
If you got the input from a user then you need to do more: it’s not enough to replace a backslash because that’s not how the characters are stored internally, the \uXXXX is an escape sequence representing a Unicode code point.
If you want to replace a user input escape sequence by a Unicode code point you need to parse the user input properly. You can use a regular expression for that:
MatchEvaluator replacer = m => ((char) int.Parse(m.Groups[1].Value, NumberStyles.AllowHexSpecifier)).ToString();
string result = Regex.Replace(name, #"\\u([a-fA-F0-9]{4})", replacer);
This matches each escape group (\u followed by four hex digits), extracts the hex digits, parses them and translates them to a character.

Include "\" in a C# string

I'm trying to set the value of a string to something that has a \ in it, but cannot do so as they say I have an unrecognized escape sequence. Is it possible to write \ in a string?
You must escape it... if you are using a regular string you must double the slash "hello\\world" or if you want it as a literal you can use #"hello\world"
Yes, just change the \ to a \\.
You can read more about Escape Sequences here.
All the above answers are right. I want to include one more way of doing the same i.e. by using a unicode character.
e.g. the \u005c represents "\"
hence "hello \u005c world"; will give the output as hello \ world
All the below will give the same result
string test1 = "hello \\ world";
string test2 = #"hello \ world";
string test3 = "hello \u005c world";
For a list of unicode character set visit this site
Thanks
like others have pointed out, use double slash "\\"
OR you can change your string to a string literal, and not have to update your slashes...
eg
string a = #"some s\tring wi\th slashes";
Alternatively, you can prefix the string with #, which will tell the compiler to interpret the string literally.
string str = #"i am using \ in a string";
Yes, use "\\".
For an explanation and a list of possible escape symbols, see http://msdn.microsoft.com/en-us/library/ms228362.aspx .

Only keep the legal chars in a text using a .NET Regex

I have a list of legal characters and I want to remove all others chars from text.
// my legal chars. a-Z, numbers, space, _, - and percentage
string legalChars = "[\p{L}\p{Nd}_\- %]*"
string text = "[update], Text with {illegal} chars such as: !? {}";
I do find a lot of examples for removing illegal chars. I want to do the opposite.
How about:
String trimmed = Regex.Replace(input, #"[^\p{L}\p{Nd}_\- %]", "");
Or:
private static readonly Regex RemovalPattern
= new Regex(#"[^\p{L}\p{Nd}_\- %]");
...
string trimmed = RemovalPattern.Replace(input, "");
Note that your regex of legal characters currently doesn't include space, contrary to the comment.
Why not loop through the string yourselfa and check for each character if it's a legal char append the char to a new string (for example with stringbuilder)

Categories