Save program output to a (.txt) file - c#

My program
namespace trim2
{
class Program
{
static void Main(string[] args)
{
//ask user to start, if yes then continue, else then close
DataConversion()
}
public static void DataConversion()
{
string lines = File.ReadAllText(#"d:\trim1.txt");
string result;
result = lines.Replace("- - ", string.Empty).Replace("+", string.Empty).....
\\process goes here
\\process goes here
\\process goes here
}
}
}
What I expected is after the file goes thru the data conversion process it would save into a new text file (which is the processed one).. how can i achieve this?
Also, I tried this line, seems wont work
File.WriteAllText("C:\Users\Cleaned.txt", new string(ShiftLine));

The \ character is used to designate an escape sequence
Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.
To avoid that interpretation of the string you either you need to write it twice:
File.WriteAllText("C:\\Users\\Cleaned.txt", new string(ShiftLine));
or you use the # operator to tell the compiler to interpret the string literally:
File.WriteAllText(#"C:\Users\Cleaned.txt", new string(ShiftLine));
# is a verbatim string literal
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,

Related

UTF-16 error, how to solve the unrecognized escape sequence?

This program is a translator program that takes some symbols and converts them to normal letters.
The problem is, when I try to put some symbols like: allAlphabets.Add("[]/[]"); or: allAlphabets.Add("//"); , i get an error about the UTF-16
static void Main(string[] args)
{
string input = ""; // string input
List<string> allAlphabets = new List<string>(); // storing to a list
input = Console.ReadLine();
char[] word = input.ToCharArray();
for (int i = 0; i < word.Length; i++)
{
switch (word[i]) // switch casce
{
normal letters
case 'm':
allAlphabets.Add("[]\/[]"); // represents text as a sequence of utf-16 code units
break;
case 'n':
allAlphabets.Add("[]\[]"); // represents text as a sequence of utf-16 code units
case 'v':
allAlphabets.Add("\/"); // represents text as a sequence of utf-16 code units
break;
case 'w':
allAlphabets.Add("\/\/"); // represents text as a sequence of utf-16 code units
}
}
}
}
Does someone know a way of encoding the unrecognized escape sequence?
Thank you!
You need to use the verbatim identifier (#)
To indicate that a string literal is to be interpreted verbatim. The #
character in this instance defines a verbatim string literal. Simple
escape sequences (such as "\\" for a backslash), hexadecimal escape
sequences (such as "\x0041" for an uppercase A), and Unicode escape
sequences (such as "\u0041" for an uppercase A) are interpreted
literally. Only a quote escape sequence ("") is not interpreted
literally; it produces a single quotation mark. Additionally, in case
of a verbatim interpolated string brace escape sequences ({{ and }})
are not interpreted literally; they produce single brace characters.
allAlphabets.Add(#"[]\/[]");
or escape the backslash
allAlphabets.Add("[]\\/[]")
Additional Resources
Strings (C# Programming Guide)
Regular and Verbatim String Literals
String Escape Sequences

Reversing a string with escape characters

I have a string that might contain escape characters. Let's assume this is '\'. I follow the MSDN Escape Sequences definition
I want to reverse this string, but keep the escape sequences.
Example:
string input = #"Hello\_World";
string reversed = #"dlroW\_elloH";
Note that in my input string the backslashes are separate characters. The reversed string is meant to be used in a SQL LIKE statement where the underscore is not meant as a wild card, but literally as an underscore. The backslash in the SQL LIKE functions as an escape character
The problem is, that if a character in my original string is preceded by a backslash, then in my reversed string this backslash should still precede the character: #"_" (two separate characters) should in reverse still be #"_".
Bonus points: Reverse escape sequences with numbers '\x0128'
I've tried it as extension functions:
public static string EscapedReverse(this string txt, char escapeChar)
{
IList<char> charList = txt.ToList();
return new string(EscapedReverse(charList, escapeChar).ToArray());
}
public static IEnumerable<char> EscapedReverse(this IList<char> text, char escapeChar)
{
int i = text.Count-1;
// Text[i] is the last character of the sequence;
// text[i] is the next character to return, except if text[i-1] is escapeChar
while (i > 0)
{
if(text[i-1] == escapeChar)
{
yield return text[i-1];
yield return text[i];
i -= 2;
}
else
{
yield return text[i];
i -= 1;
}
}
// return the last character
if (i == 0)
yield return text[i];
}
This works. However, my string is converted to array / list twice. I wondered if there would be a smarter method where the elements don't have to be accessed so often?
Addition: what is my problem anyway?
Comments suggested to add more information about my problem.
There is a requirement to show a list of matching elements while an operator is typing in a text box. Most elements he can see start with a similar prefix. The difference the operator searches for is in the end of the name.
Therefore we want to show a list of names ending with the typed character. So if the operator types "World" he will see a list with all names ending with "World".
The already existing database (change is out of the question) has a table with a NAME and a REVERSEDNAME. Software takes care that if a name is inserted or updated the correct reversed name is inserted / updated. REVERSEDNAME is indexed, so using a WHERE with reversed name is fast.
So if I need to return all names ending with "World", I need to return the names of all records where the REVERSEDNAME starts with the reverse of "WORLD":
SELECT TOP 30 [MYTABLE].[NAME] as Name
FROM [MYTABLE]
WHERE [MYTABLE].REVERSEDNAME LIKE 'dlroW%'
This works fine as long as no wild cards (like underscore) are used. This was solved by the software by escaping the underscore character (I know, bad design, the fact that SQL LIKE uses underscore as wild card should not seep through, but I have to live with this existing software)
So the operator types #"My_World"
My software received #"My_World", the backslash is a separate character
I have to reverse to #"dlrow_yM", note that the backslash is still before the underscore
My Dapper code:
IEnumerable<string> FetchNamesEndingWith(string nameEnd)
// here is my reversal procedure:
string reversedNameEnd = nameEnd.EscapedReverse() = '%';
using (var dbConnection = this.CreateOpenDbConnection())
{
return dbConnection.Query<string>(#"
SELECT TOP 30 [MYTABLE].[NAME] as Name
FROM [MYTABLE]
WHERE [MYTABLE].REVERSEDNAME LIKE #param ESCAPE '\'",
new {param = reversedNameEnd});
}
MSDN about using escape characters in SQL LIKE
Changing the escape character to a different character doesn't help. The problem is not that the escape character is a backslash, but that reversing my string should keep the escape character in front of the escaped character.
My code works, I only wondered if there would be a better algorithm that doesn't copy the string twice. Not only for this specific problem, but also if in future problems I need to reverse strings and keep certain characters in place.
You can use regular expressions:
var pattern = #"\\x[1-9a-fA-F]{4}|\\x[1-9a-fA-F]{2}|\\[0-7]{3}|\\.|.";
var rgx = new Regex(pattern);
return new string(
rgx.Matches(txt)
.Cast<Match>()
.OrderByDescending(x => x.Index)
.SelectMany(x => x.Value)
.ToArray());
pattern covers single characters and escape sequences in formats:
\x????
\x??
\???
\?

Check a String for a Slash

string testStr="thestringhasa\slash";
if(testStr.Contains("\"))
{
//Code to process string with \
}
How do I properly test to see if a string contains a backslash, when I try the it statement if says a New Line in constant.
The other two answers are entirely correct, but no one bothered to explain why. The \ character has a special purpose in C# strings. It is the escape character, so to have a string that contains a slash, you have to use one of two methods.
Use the string literal symbol #. A string preceded by the # symbol tells the C# compiler to treat the string as a literal and not escape anything.
Use the escape character to tell the C# compiler there is a special character that is actually part of the string.
So, the following strings are equivalent:
var temp1 = #"test\test";
var test2 = "test\\test";
test1 == test2; // Yields true
You should use double slashes
string testStr=#"thestringhasa\slash";
if(testStr.Contains("\\"))
{
//Code to process string with \
}
The backslash must be escaped. Try the following:
string testStr = #"thestringhasa\slash";
if (testStr.Contains("\\"))
{
//Code to process string with \
}

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.

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

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

Categories