How can I remove the black diamond question mark when I read a text file?
In the text file the real character is • but after reading the text file it became �. I don't know how to manipulate/delete that character.
This character means, that you are reading the file in the wrong encoding. At first you need to know how your file is encoded. You could get this informatio from Notepad++ for example. Then in your code read the file with this encoding specified. Here is an example to read unicode files:
var text = File.ReadAllText(filePath, Encoding.Unicode);
Remove character:
text.Replace("•", string.Empty);
You could just set a conditional for that character. Then use the Remove method. something like this:
if(inputstring.StartsWith('�')
{
inputstring = inputstring.Remove(0,1);
}
The black diamond with a question mark is a place-holder for unrecognized characters. You can use Regex to replace • with an empty string:
//...
using System.Text.RegularExpressions;
using System.IO;
//...
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "file.txt");
string yourString = Regex.Replace(File.ReadAllText(filePath, "file.txt")), #"\u2022", "");
Since the • character's value is u2022, the Regex pattern will match it and replace that with an empty string. Regex.Replace() takes 3 arguments in this case: the source string, the expression, and the string to replace the text that matchs the pattern.
I tested it out and it worked just fine:
Contents of file.txt: hello, •world!
After using Regex to replace •:
File.ReadAllLines(pathTxt, Encoding.GetEncoding(28591));
Related
In order to be able to read a file in asp.net, the file path must be written in this:
1.
C:\\yung\\Desktop
returns
however, the string that the fileUpload get returns is
2.
C:\yung\Desktop
After reading the comments i have this code:
string FilePath = FileUploadPublicInfo.PostedFile.FileName;
System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
string line = File.ReadLines(FilePath.ToString()).Skip(4).ToString();
TextBox1.Text = line.ToString();
But now its giving this error:
System.Linq.Enumerable+<SkipIterator>d__30`1[System.String]
How to solve this problem?
Thank you.
I'm not so sure I understand the question, but I think you are looking for string.Replace:
string DoubleSlash(string singleSlash)
{
return singleSlash.Replace(#"\", #"\\");
}
The reason backslashes disappear is that C# compiler treats slashes in string literals as a special "escape" character. Because of this treatment, backslash needs to be encoded as two slashes in a regular string literal.
C# offers two ways of inserting backslashes the way you need:
Use verbatim literals - prefix it with "at" sign, i.e. #"C:\\yung\\Desktop", or
Double each slash - put two slashes for each slash in the result: C:\\\\yung\\\\Desktop
Ok, i have manage to solve this problem, turns out it was not reading anything.
This is the code that i finally get:
This is to retrieve the File's path, using this, would give the file path will double slash, so there is not a need for Replace(#"\",#"\")
string FilePath = FileUploadPublicInfo.PostedFile.FileName;
Then read the specified file
System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
If you know which line you specifically want, this retrieves the 5th line
string line = File.ReadLines(FilePath.ToString()).Skip(4).First().ToString();
Thank you so much for your help...
I am reading text from a .TXT file in a String. I am using File.ReadAllText.
string Str= File.ReadAllText(#"C:\temp\file.txt", Encoding.Default);
Let's assume the Str contains following string.
string Str= #"one
two
three";
Now the problem is I cannot find the newline characters from Str.
string[] lines = Str.Split('\n');
foreach(string line in lines)
{
Console.WriteLine(line.IndexOf('\n'); // prints -1 three times
}
Is there any way I can find newline character in this situation? Please suggest.
.Split will delete the delimiter characters, and the resulting output will not include them.
From MSDN:
Delimiter characters are not included in the elements of the returned
array.
If you need to find the length of a line, just use the .Length property of the string.
In any case, as mentioned in the comments, use the File.ReadAllLines method to avoid having to split the file contents yourself.
Per MSDN documentation for the String.Split method:
Delimiter characters are not included in the elements of the returned
array.
Just as an observation, if you are trying to load a file and process it line by line you may want to do something like this:
foreach (var line in File.ReadLines(#"C:\temp\file.txt"))
{
Console.WriteLine(line);
}
I have a xml string shown below:
String xml = #"<axislable='ihgyh\nuijh\nkjjfgj'>";
Now when I try to output the xml it shows <axislable='ihgyh\nuijh\nkjjfgj'>
But my requirement is to break the line like below
<axislable='ihgyh
uijh
kjjfgj'>
I have tried replacing the xml using xml = xml.Replace("\n", "\\n"); But it doesnt seems to work.Any ideas how to break the line?
Regards,
Sharmila
Don't use the # prefix:
var xml = "<axislable='ihgyh\nuijh\nkjjfgj'>";
Also you may need "\r\n" instead:
var xml = "<axislable='ihgyh\r\nuijh\r\nkjjfgj'>";
It's not working since you have no line breaks in your string. Your string contains the substring "\n".
Notice you use the # operator.
Try the following:
xml = xml.Replace("\\n", "\n");
Try
xml.Replace(#"\n", Envioroment.NewLine);
Similar to Leo's answer (sorry don't know how to comment on your answer)
String xml = String.Format("<axislable='ihgyh{0}uijh{0}kjjfgj'>", Environment.NewLine);
The # character means that the string is a verbatim string, meaning that escape characters like \n in the string are not processed and treated as text.
The following string is a regular string:
string xml="<axislable='ihgyh\nuijh\nkjjfgj'>";
and translates the \n escape sequence to a newline as you would expect.
You should check the documentation on string literals for the difference between the two forms.
I have a file that is supposed to be \n\n delimited, but of course its not. Some of the lines contains spaces after the \n\n. How do I find a remove all spaces after a \n\n that starts a new line but that is before any other character.
Sample:
\n\nData,Mo re,Data
\n\n Some,Li st,Of
\n\n\nOther,St uff
\n\n\n\n This is another
Desired Output
\n\nData,Mo re,Data
\n\nSome,Li st,Of
\n\nOther,St uff
\n\nThis is another
Regex is probably the answer, but I'm still learning regex. Here's more or less what I've come up with Regex.Replace(input,"^(\n{2,}\s*)", "\n\n") but it doesn't work.
Edit: I should note that I pre-convert from various different line break encodings to \n before this code is needed.
The backslash character needs escaping. Try:
Regex.Replace(input,"^(\n{2,}\\s*)", "\n\n")
Also, you should consider changing \\s* to \\s+ so you don't replace valid line starts unnecesarily.
string test = "\n\nData,Mo re,Data \r\n\n\n Some,Li st,Of \r\n\n\n\nOther,St uff \r\n\n\n\n\n This is another \r\n";
string pattern = "^\n{2,}\\s*";
string result = Regex.Replace(test, pattern, "\n\n", RegexOptions.Multiline);
First, you need the Multiline option. Second, how does your data really look? Notice that where you have a visible cr-lf, I put in \r\n. I did so because you said that it is a \n\n at the beginning of a line that delimits the data. The \n is confusing to work with, so be sure of your data.
I'm trying to read a text file, and then break it up by each line thats is split by a "\n". Then Regex it and write out the regex.
string contents = File.ReadAllText(filename);
string[] firefox = filename.Split("\r\n");
string prefix = prefix = Regex.Match(firefox, #"(\d)").Groups[0].Value;
File.AppendAllText(workingdirform2 + "configuration.txt", prefix);
string[] firefox = filename.Split("\r\n"); doesnt exactly work.
What I want to do is run a regex foreach line of contents and then write out each line after the regex
So...
filename:
Hero123
Hero243
Hero5959
writes out to:
13
243
5959
Well everybody is suggesting something off the base in which i started. the ending result will be about a 20 line regex with Ints. I've got to parse it out line by line.
File.ReadAllLines
var lines = File.ReadAllLines(originalPath);
File.WriteAllLines(newPath, lines
.Select(l => Regex.Match(l, #"\d+").Value).ToArray());
There are a number of problems with your code:
The reason the splitting doesn't work, is because you're splitting filename, not contents, which contains the actual file data. I agree with the other poster on using File.ReadAllLines :) It's a little more flexible with the file format compared to using \r\n, amongst other things.
Also, you have string prefix = prefix = ..., the second equals sign is probably intended to be a +. You should using StringBuilder if the data files can become large, or better yet, write to an output stream as you go.
Passing an array to Regex.Match doesn't work either. To apply the regex to all lines, you should do something like:
foreach (string line in firefox)
{
prefix = prefix + Regex.Match(line, // etc
// Or rather:
// stringBuilder.AppendLine(...)
}
Either that, or do it all at once with a multiline regex :)