Replce single slash to double slash is not working always return the single slash..
string input;
input = "\r\t";
string mat1= input.Replace("\\\\","\\\\\\\\");
string inputt= mat1;
if i am run the above code it will return output is \r\t only....
but i need output like this
\r\t
"\r\t" is in fact just two characters, carriage return and tab. This is because the \ escape character is used to specify special characters.
If you want to have a string that is actually "\r\t" you need to escape the \ characters by using \\.
So your string should be:
input = "\\r\\t";
Or
input = #"\r\t";
And then to replace the backslashes with double backslashes:
string mat1= input.Replace("\\","\\\\");
Or
string mat1= input.Replace(#"\", #"\\");
input = "\r\t";
is a so called escaped string. \ means escape sequence. if you need \r\t you need to write
input = "\\r\\t";
This is 2 already escaped characters, not 4.
input="\r\t";
\r and \t are special literals. Check this article:
\r - Carriage return
\t - Horizontal tab
What you wanted, I gess, is to change this special literal like this:
string input;
input = "\r\t";
input = input.Replace("\r", "\\r");
input = input.Replace("\t", "\\t");
Console.WriteLine(input);
Related
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.
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';
I need to replace double quotes with single so that something like this
\\\\servername\\dir1\\subdir1\\
becomes
\\servername\dir1\subdir1\
I tried this
string dir = "\\\\servername\\dir1\\subdir1\\";
string s = dir.Replace(#"\\", #"\");
The result I get is
\\servername\\dir1\\subdir1\\
Any ideas?
You don't need to replace anything here. The backslashes are escaped, that's why they are doubled.
Just like \t represents a tabulator, \\ represents a single \. You can see the full list of Escape Sequences on MSDN.
string dir = "\\\\servername\\dir1\\subdir1\\";
Console.WriteLine(dir);
This will output \\servername\dir1\subdir1\.
BTW: You can use the verbatim string to make it more readable:
string dir = #"\\servername\dir1\subdir1\";
There is no problem with the code for replacing. The result that you get is:
\servername\dir1\subdir1\
When you are looking at the result in the debugger, it's shown as it would be written as a literal string, so a backslash characters is shown as two backslash characters.
The string that you create isn't what you think it is. This code:
string dir = "\\\\servername\\dir1\\subdir1\\";
produces a string containing:
\\servername\dir1\subdir1\
The replacement code does replace the \\ at the beginning of the string.
If you want to produce the string \\\\servername\\dir1\\subdir1\\, you use:
string dir = #"\\\\servername\\dir1\\subdir1\\";
or:
string dir = "\\\\\\\\servername\\\\dir1\\\\subdir1\\\\";
This string "\\\\servername\\dir1\\subdir1\\" is the same as #"\\servername\dir1\subdir1\". In order to escape backslashes you need either use # symbol before string, or use double backslash instead of one.
Why you need that? Because in C# backslash used for escape sequences.
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.
Hi I have this problem. From server I get JSON string as unicode escape sequences an I need convert this sequences to unicode string. I find some solution, but any doesn’t work for all json response.
For example from server I get this string.
string encodedText="{\"DATA\":{\"idUser\":18167521,\"nick\":\"KecMessanger2\",\"photo\":\"1\",\"sex\":1,\"photoAlbums\":0,\"videoAlbums\":0,\"sefNick\":\"kecmessanger2\",\"profilPercent\":0,\"emphasis\":false,\"age\":25,\"isBlocked\":false,\"PHOTO\":{\"normal\":\"http://213.215.107.125/fotky/1816/75/n_18167521.jpg?v=1\",\"medium\":\"http://213.215.107.125/fotky/1816/75/m_18167521.jpg?v=1\",\"24x24\":\"http://213.215.107.125/fotky/1816/75/s_18167521.jpg?v=1\"},\"PLUS\":{\"active\":false,\"activeTo\":\"0000-00-00\"},\"LOCATION\":{\"idRegion\":\"1\",\"regionName\":\"Banskobystricku00fd kraj\",\"idCity\":\"109\",\"cityName\":\"Rimavsku00e1 Sobota\"},\"STATUS\":{\"isLoged\":true,\"isChating\":false,\"idChat\":0,\"roomName\":\"\",\"lastLogin\":1291898043},\"PROJECT_STATUS\":{\"photoAlbums\":0,\"photoAlbumsFavs\":0,\"videoAlbums\":0,\"videoAlbumsFavs\":0,\"videoAlbumsExts\":0,\"blogPosts\":0,\"emailNew\":0,\"postaNew\":0,\"clubInvitations\":0,\"dashboardItems\":26},\"STATUS_MESSAGE\":{\"statusMessage\":\"Nepru00edtomnu00fd.\",\"addTime\":\"1291887539\"},\"isFriend\":false,\"isIamFriend\":false}}";
statusMessage in jsonstring consist Nepru00edtomnu00fd, in .net unicode string is it Neprítomný.
region in jsonstring consist Banskobystricku00fd in .net unicode string is it BanskoBystrický.
Other examples:
Nepru00edtomnu00fd -> Neprítomný
Banskobystricku00fd -> BanskoBystrický
Trenu010du00edn -> Trenčín
I need convert unicode escape sequences to .net string in slovak language.
On converting I used this func:
private static string UnicodeStringToNET(string input)
{
var regex = new Regex(#"\\[uU]([0-9A-F]{4})", RegexOptions.IgnoreCase);
return input = regex.Replace(input, match => ((char)int.Parse(match.Groups[1].Value,
NumberStyles.HexNumber)).ToString());
}
Where can be problem?
Here's a method (based on previous answers) that I wrote to do the job. It handles both \uhhhh and \Uhhhhhhhh, and it will preserve escaped unicode escapes (so if your string needs to contain a literal \uffff, you can do that). The temporary placeholder character \uf00b is in a private use area, so it shouldn't typically occur in Unicode strings.
public static string ParseUnicodeEscapes(string escapedString)
{
const string literalBackslashPlaceholder = "\uf00b";
const string unicodeEscapeRegexString = #"(?:\\u([0-9a-fA-F]{4}))|(?:\\U([0-9a-fA-F]{8}))";
// Replace escaped backslashes with something else so we don't
// accidentally expand escaped unicode escapes.
string workingString = escapedString.Replace("\\\\", literalBackslashPlaceholder);
// Replace unicode escapes with actual unicode characters.
workingString = new Regex(unicodeEscapeRegexString).Replace(workingString,
match => ((char) Int32.Parse(match.Value.Substring(2), NumberStyles.HexNumber))
.ToString(CultureInfo.InvariantCulture));
// Replace the escaped backslash placeholders with non-escaped literal backslashes.
workingString = workingString.Replace(literalBackslashPlaceholder, "\\");
return workingString;
}
Your escape sequences do not start with a \ like "\u00fd" so you Regex should be only
"[uU]([0-9A-F]{4})"
...