I am just curious about the title.
String a = "abc\ndef";
Console.Writeline(a);
The output is
abc
def
Then I stored that value into an ini file and retrieved it from there.
ini.iniwritevalue("a", "a", a);
string b = ini.inireadvalue("a", "a");
Then I showed it on the console. The result is the following:
abc\ndef
Why is \n not working after I retrieved it from the ini file?
P.S. I have a ini.dll file. Our company is using that dll to read and write ini files.
The interpretation of the \n escape code in your source code is done by the compiler when parsing the source file.
If you just read in a file as "data" at runtime, no such interpretation is necessarily going to occur.
You may need to find or write a function which takes a string containing escape sequences and converts them to binary values (\n usually becomes 0x0a)
This is because \n in C# is not just a \ and an n, but an escape sequence with a special meaning. \n is considered a single character and is a line ending. You will not get it when you simply read a \ and an n from a file.
Possibly, you read \\n from there. \\ is also an escape sequence which means the \ character. All you have to do is replace \\n with \n, and it'll be okay.
string s = ... //get the value
s = s.Replace("\\n", "\n");
You need to escape the slash when you write the value, like this:
abc\\ndef
Related
I'm working on application where my goal is to import a data from .txt file and store it in database.
One row in that file looks like this: .txt file for import
Lets take a look at "Papua New Guinea" which I marked with red square in the previous image.
So after importing this file using IFormFile I get something like this: List of items in code
My plan is to store this values to database, but I am having redudant characters as can be seen in previous picture "\"Papua New Guinea\"".
How can I remove those redudant characters? Having in mind that not every item will have those redudant (\") characters (2nd image you can see some integer values)
The \ is used as escape character to show you that the following " does not mark the end of the string but is that it is part of it. The escape character is not part of the string. I.e., in "\"Papua New Guinea\"", the string begins with the characters ", P, a and ends with e, a, ".
You can use an overload of Trim accepting characters to be trimmed from the beginning and the end of the string.
string s = "\"Papua New Guinea\"";
string trimmed = s.Trim('"');
Note that since character literals are delimited with single quotes, you can specify the double quote character without escape character.
Those slashes wont' actually show up if you try to print or do anything with the string. They're just there because strings start with a " and \" is a way of adding a quotation mark to a string. Just like \n adds a new line. The integers don't have them because they don't include a quotation mark.
In my current project, the user writes a file path (Example: "C:\Data") into a Textbox. Then I read it with:
string PathInput = tbPath.Text;
And then send it into an SQL Insert Query.
If I then read the data from SQL, I get back: C:Data
So I tried to do:
string Path = PathInput.Replace(#"\", "\\");
So that it would double the \\, because when I enter C:\\Data I get C:\Data. But it looks like the \ get lost in Textbox and not in Database.
So, how can I read the TextBox without losing the \s?
Your replace doesn't actually replace anything:
PathInput.Replace(#"\", "\\");
Since you use an # before the first string, you don't have to escape anything. But in the second string, you don't use #, meaning you have to escape characters in that string - that means you're replacing the \ with another \.
Change it to:
PathInput.Replace(#"\", #"\\");
I am trying to grab a handle to a file that has unicode characters in the filename.
For example, I have a file called c:\testø.txt. If I try new FileInfo("c:\testø.txt") I get an Illegal characters exception.
Trying again with an escape sequence: new FileInfo("c:\test\u00f8.txt") and it works! Yay!
So I've got a method to escape non-ASCII characters:
static string EscapeNonAsciiCharacters(string value)
{
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
if (c > 127)
{
// This character is too big for ASCII
string encodedValue = "\\u" + ((int)c).ToString("x4");
sb.Append(encodedValue);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
But when I take the output from this method the escape characters seem to be incorrect.
EscapeNonAsciiCharacters("c:\testø.txt") ## => "c:\test\\u00f8.txt"
When I pass that output to the FileInfo constructor, I get the illegal chars exception again. However, the \ in c:\ seems to be unaltered. When I look at how this character is represented within the StringBuilder in the static method, I see: {c: est\u00f8.txt} which leads me to believe that the first backslash is being escaped differently.
How can I properly append the characters escaped by the loop in EscapeNonAsciiCharacters so I don't get the double escape character in my output?
You have more escaped in those strings than you probably intend.
Note that \ needs to be escaped when in a string, because it is itself the escape character and \t means tab.
Windows, using NTFS, is fully unicode-capable, so the original error is most likely due to you not escaping the \ character.
I wrote a toy application to deal with the file named ʚ.txt, and the constructor has no problem with that or any other unicode characters.
So, instead of writing new FileInfo("c:\testø.txt"), You need to write new FileInfo("c:\\testø.txt") or new FileInfo(#"c:\testø.txt").
Your escape function is entirely unnecessary in the context of C# in general and NTFS (or, really, most modern file systems). External libraries may, themselves, have incompatibilities with unicode, but that will need to be dealt with separately.
You seem to be misunderstanding escaped characters.
In this C# code, it is the compiler that converts the \u00f8 to the correct unicode character:
new FileInfo("c:\test\u00f8.txt") // (the "\t" is actually causing an error here)
What you are doing here is just setting encodedValue to the string "\u00f8", and there is nothing ever converting the escaped string to the converted string:
string encodedValue = "\\u" + ((int)c).ToString("x4");
If you want to convert the escaped string, then you need to do something like this:
How to convert a string containing escape characters to a string
I have to parse some files that contain some string that has characters in them that I need to escape. To make a short example you can imagine something like this:
var stringFromFile = "This is \\n a test \\u0085";
Console.WriteLine(stringFromFile);
The above results in the output:
This is \n a test \u0085
, but I want the text escaped. How do I do this in C#? The text contains unicode characters too.
To make clear; The above code is just an example. The text contains the \n and unicode \u00xx characters from the file.
Example of the file contents:
Fisika (vanaf Grieks, \u03C6\u03C5\u03C3\u03B9\u03BA\u03CC\u03C2,
\"Natuurlik\", en \u03C6\u03CD\u03C3\u03B9\u03C2, \"Natuur\") is die
wetenskap van die Natuur
Try it using: Regex.Unescape(string)
Should be the right way.
Att.
Don't use the # symbol -- this interprets the string as 100% literal. Just take it off and all shall be well.
EDIT
I may have been a bit hasty with my reply. I think what you're asking is: how can I have C# turn the literal string '\n' into a newline, when read from a file (similar question for other escaped literals).
The answer is: you write it yourself. You need to search for "\\n" and convert it to "\n". Keep in mind that in C#, it's the compiler not the language that changes your strings into actual literals, so there's not some library call to do this (actually there could be -- someone look this up, quick).
EDIT
Aha! Eureka! Behold:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.unescape.aspx
Since you are reading the string from a file, \n is not read as a unicode character but rather as two characters \ and n.
I would say you probably need a search an replace function to convert string "\n" to its unicode character '\n' and so on.
I don't think there's any easy way to do this. Because it's the job of lexical analyzer to parse literals.
I would try generating and compiling a class via CodeDOM with the string inserted there as constant. It's not very fast but it will do all escaping.
In a database field, I'm storing and returning the "body" of my email (in case it changes). In this I have \n\r characters to get new lines. But, it seems not to be working. Any thoughts:
So data in field:
'TBD.\r\n\nExpect'
And my output looks like (literal \r and \n):
TBD.\r\n\nExpect
Thoughts?
Escape sequences have no meaning within actual string objects - only when the C# parser/compiler interprets them. You want to store an actual new line in your database field rather than the 4 characters "\r\n".
It is likely that the \r\n is escaped, so the string actually returned would be equivalent to a string
myString = "\\r\\n";
So you would need to remove these extra slashes either when adding or removing from the database.
Though likely unrelated to your problem, the extra \n you have may cause viewing problems depending on the system, editor, etc.
You could replace all occurrences of \\n\\r, etc. using:
replacedString = myString.Replace("\\r\\n", "\r\n");
This should work to fix your problem.
Because \r, \n, etc. works only within a string in your C# code. If you read a string from a file, a database, or other things, they just get the verbatim values...
Replace your \r\n with System.Environment.NewLine as the below may do work for you:
text.Replace("\r\n", System.Environment.NewLine);