Showing Octal characters in c# [duplicate] - c#

This question already has answers here:
Octal equivalent in C#
(5 answers)
Closed 6 years ago.
According to MSDN:
\ooo ASCII character in octal notation
The following code is showing Octal character($) in c#:
char character36 = '\o44';
Console.Write(character36);
but it doesn't work.

In Escape Sequences, which I suspect you were reading, the "ooo" is italicised to indicate that it should not be included verbatim. It should be replaced by the appropriate octal digits. "o44" aren't octal digits, you included a literal "o". The appropriate octal digits would be "044", or just "44".
But as pointed out by Andrew Savinykh, this is a documentation page about C, not about C#, and C# does not use the same syntax. It doesn't have octal escape sequences at all. The escape sequences for C# are documented on Strings (C# Programming Guide), and do not include any octal escape sequences unless you want to include the special exception of \0. You can use hexadecimal escape sequences instead, either \u0024 or \x24.

Related

regex in C# not working properly for numeric check [duplicate]

This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 2 years ago.
I tried to use regexes in C#
^(?=.*\d)(?=.*[a - z])(?=.*[A - Z])(?=.*[!##$%^*])(?=.*[a-zA-Z]).{6,20}$
but \d comes as an error if i put [0-9] instead it wont work as desired
This should check the string has a uppercase, lowercase, symbol and a number
You should use [0-9]. Probably it is more correct... \d will catch non-european digits like ७ (it is a Devanagari digit).
For the reason:
you probably wrote:
var rx = new Regex("\d");
But in this way the \d is an escape sequence of the string instead of being a regex.
Write
var rx = new Regex(#"\d");
to deactivate the escape expansion of strings.

Parse chars in escape sequence form [duplicate]

This question already has answers here:
Can I expand a string that contains C# literal expressions at runtime
(5 answers)
Closed 3 years ago.
I'm implementing a compiler, and I need to convert from an escape character literal written in source code file to an actual value.
For example I might have a line of source code char = '\\'. I then parse that and get given the string "'\\'", which I need to turn into the actual char '\\'.
char.Parse and char.TryParse both fail when parsing a char in escape sequence form. For example:
char.Parse(#"\\");
Will throw "String must be exactly one character long."
Is there any way to parse everything on this list as a char (ignoring those that are to big to fit in a UTF16 char).
the # makes it a verbatim string literal. drop that and \ will be treated as the escape char for the next \.
#"\\".Length // 2
"\\".Length // 1

How to add {,},[,] brackets to regex pattern to allow them as special characters [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I am having this below regex pattern. I want to allow these below characters as special characters.
{
}
[
]
Regex regEx = new Regex(#"^(?=.*[A-Z])(?=.*[!##$%^&*()+_<>~-])(?=.*[0-9])(?=.*[a-z]).{8,15}$");
Is there a way to add them
You'll need to escape them within the regex. Within a regular expression, you can refer to "\[" to get a '[' character, for example.
Note that in C, C#, C++, Java, etc, you usually need to double up the '\' character to escape the escape character. So when adding '[' to your regex, you would actually use "\\[". In your case you're using an #"" so you've escaped that trap.

Regex to only accept Numeric and Decimal Value not any special or character in c# [duplicate]

This question already has answers here:
Regular expression to match numbers with or without commas and decimals in text
(11 answers)
Closed 5 years ago.
How do I restrict special characters and character in a Textbox?
I'm using this code but I am not restricting special characters and character
code :-
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, #"^\\d*\\.?\\d*$"))
{
// Write Code
}
if you're using # in front of string, you don't have to escape characters. So, remove all those extra backslash chars and your regex should work. Like this:
if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, #"^\d*\.?\d*$"))
EDIT:
use sites like this to test your regex.

C# Convert Unicode chars [duplicate]

This question already has answers here:
Unicode characters string
(5 answers)
Closed 6 years ago.
is it possible to convert a string like "\u00e8" (Got it by reading a WebRequestResponse with Streamreader) to it's unicode char(è)?
Tried many things with Encoding, but nothing works.
You can use Regex.Unescape(), which unescapes any escape sequences that are valid for a Regex (including \uXXXX). Note that it also unescapes other sequences like \t, \n, and \[.

Categories