How can I concatenate the string "\u" with "a string" to get "\u0000"?
My code creates two backslashes:
string a = #"\u" + "0000"; //ends up being "\\\u0000";
The escape sequence \uXXXX is part of the language's syntax and represents a single Unicode character. By contrast, #"\u" and "0000" are two different strings, with a total of six characters. Concatenating them won't magically turn them into a single Unicode escape.
If you're trying to convert a Unicode code point into a single-character string, do this:
char.ConvertFromUtf32(strUnicodeOfMiddleChar).ToString()
BTW, don't use == true; it's redundant.
If I understand you correctly, I think you want to build a single-char string from an arbitrary Unicode value (4 hex digits). So given the string "0000", you want to convert that into the string "\u0000", i.e., a string containing a single character.
I think this is what you want:
string f = "0000"; // Or whatever
int n = int.Parse(f, NumberStyles.AllowHexSpecifier);
string s = ((char) n).ToString();
The resulting string s is "\u0000", which you can then use for your search.
(With corrections suggested by Thomas Levesque.)
the line below creates tow backslash:
string a = #"\u" + "0000"; //a ends up being "\\u0000";
No, it doesn't; the debugger shows "\" as "\", because that's how you write a backslash in C# (when you don't prefix the string with #). If you print that string, you will see \u0000, not \\u0000.
Nope, that string really has single backslash in. Print it out to the console and you'll see that.
Escape your characters correctly!!
Both:
// I am an escaped '\'.
string a = "\\u" + "0000";
And:
// I am a literal string.
string a = #"\u" + "0000";
Will work just fine. But, and I am going out on a limb here, I am guessing that you are trying to escape a Unicode Character and Hex value so, to do that, you need:
// I am an escaped Unicode Sequence with a Hex value.
char a = '\uxxxx';
Related
How would I go about in replacing every character in a string which are not the following:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.#-
with -
So for example, the name Danny D'vito would become DannyD-vito
My inital thought was converting string to char[] and looping through and checking each character, then convert back to string. But my hunch is telling me there must be an easier way to do this
Regex.Replace() approach
string input = "Danny D'vito";
string result = new Regex("[^a-zA-Z0-9_.#-]").Replace(input, "-");
I have a parameter passed through my url which is as followed:
eu\test5.
When I print this string it says eu est5, it is replacing the \t.
How can I make sure my string is not exaping anything?
I want to grab all the data after the backslash, so I have the value test5.
string user = "eu\test5";
int backslashPos = user.IndexOf("\\");
label2.Text = user.Substring(backslashPos);
Add an # in front of the string or use "\\".
string user = #"eu\test5";
string user = "eu\\test5";
\ is used for escape sequences, which is used for several commands inside a string. Like \n for a New Line
Using an # ignores all escape sequences in a string.
Here is a list of all escape sequences link
\t is an escape sequence representing a set of spaces. So the result you are getting is obvious.
I'm getting a string as a parameter.
Every string should take 30 characters and after I check its length I want to add whitespaces to the end of the string.
E.g. if the passed string is 25 characters long, I want to add 5 more whitespaces.
The question is, how do I add whitespaces to a string?
You can use String.PadRight for this.
Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.
For example:
string paddedParam = param.PadRight(30);
You can use String.PadRight method for this;
Returns a new string of a specified length in which the end of the
current string is padded with spaces or with a specified Unicode
character.
string s = "cat".PadRight(10);
string s2 = "poodle".PadRight(10);
Console.Write(s);
Console.WriteLine("feline");
Console.Write(s2);
Console.WriteLine("canine");
Output will be;
cat feline
poodle canine
Here is a DEMO.
PadRight adds spaces to the right of strings. It makes text easier to
read or store in databases. Padding a string adds whitespace or other
characters to the beginning or end. PadRight supports any character
for padding, not just a space.
Use String.PadRight which will space out a string so it is as long as the int provided.
var str = "hello world";
var padded = str.PadRight(30);
// padded = "hello world "
you can use Padding in C#
eg
string s = "Example";
s=s.PadRight(30);
I hope It will resolve your problem.
I'm trying to convert a xml character entity to a C# char...
string charString = "₁".Replace("&#", "\\").Replace(";", "");
char c = Convert.ToChar(charString);
I have no idea why it is failing on the Convert.Char line. Even though the debugger shows charString as "\\\\x2081" it really is "\x2081", which is a valid Unicode character. The exception is too many characters.
The documentation for ToChar(string) is quite readable:
Converts the first character of a specified string to a Unicode character.
Also:
FormatException – The length of value is not 1.
It will not convert a hex representation of your character into said character. It will take a one-character string and give you that character back. The same as doing s[0].
What you want is:
string hex = "₁".Replace("&#x", "").Replace(";", "");
char c = (char)Convert.ToInt32(hex, 16);
Convert.ToChar(value) with value is a string of length 1. But charString is "\\x2081" length over 1.
Seems "₁" is Unicode Hex Character Code (Unicode Hex Character Code ₁ ). So you must do that:
string charString = "₁".Replace("&#x", "").Replace(";", "");
char c = (char)Convert.ToInt32(charString , NumberStyles.HexNumber);
Note: It's HTML Entity (hex) of SUBSCRIPT ONE (see in link above ^_^)
This is probably a really simple question but I can't seem to get my head around it. I need to have a string that contains \" without it seeing it as an escape character. I tried using # but it won't work. The only other way I thought of doing this would be to use \u0022 but don't want to unless I can help it.
Desired string - string s = "\"\""; // Obviously this doesn't work!
Desired console output - \"\"
Hope this makes sense.
Thanks!
Try
string s = "\\\"\\\"";
You have to escape your backslashes too.
Mike
You can use the literal, but you need to double-up quotes.
string s = #"\""\""";
In verbatim string literals (#"...") a " in the string value is encoded as "", which happens to also be the only escape sequence in verbatim strings.
#"\""Happy coding!\""" // => \"Happy coding!\"
"\\\"Happy coding!\\\"" // => \"Happy coding!\"
Note that in the 2nd case (not a verbatim string literal), a \ is required before the \ and the " to escape them and prevent their normal meanings.
See the C# string reference for more details and examples.
I think you have to escape backslashes too... so something like "\\\"\\\"" should work, I believe.
Use this string:
string s = "\\\"\\\"";
Console.WriteLine( "\\\"\\\"" );
Just put a \ before each character that needs to be printed.
String s = #"\""\""";
DblQuote characters will escape a second dblquote character
Though for better readability I would go with:
const String DOUBLEQUOTE = """";
const String BACKSLASH = #"\";
String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE;
In a verbatim string (a string starting with #"") to escape double quotes you use double quotes, e.g. #"Please press ""Ok"".". If you want to do it with verbatim strings then you would do something like #"\""" (that's 3 double quotes on the end there).
You can do like this,
string s = "something'\\\'";
Use a single '' rather then "" in string to do the same.