C# Detecting null characters in a string array
11,747
Solution 1
Assuming you mean a character with ascii code: 0
if (parsedLine.Length == 0 || parsedLine[0] == '\0')continue;
edit the above will work if parsedLine is a string, but for the parsing in your code:
string[] parsedLine = l.Split(new char[] { '=' },StringSplitOptions.RemoveEmptyEntries)
.Where(s=>s.Length != 1 || s[0] != '\0').ToArray();
Solution 2
Use the built-in String.IsNullOrEmpty()
method:
if (!string.IsNullOrEmpty(l))
{
// your code here
}
Solution 3
Just ignore the line if it contains the null character.
string l = sr.ReadLine();
if (string.IsNullOrEmpty(l) || l[0] == '\0'))
continue;
...
Solution 4
Here is a linq solution that should work:
StreamReader sr = new StreamReader(Convert.ToString(openFileDialog1.FileName));
while (!sr.EndOfStream)
{
string l= sr.ReadLine();
bool nullPresent = l.ToCharArray().Any(x => x.CompareTo('\0') == 0);
if (nullPresent)
{
MessageBox.Show("Ignoring line");
}
else
{
// do other stuff
}
}
Comments
-
Daniel Flannery almost 2 years
I am reading in from a text file using
StreamReader
into a string arraytext[]
. One of the lines in the textfile is being read in as"\0"
in positions 1 -> 20 of the array. How would I go about detecting this null character and ignoring this line.Code example:
StreamReader sr = new StreamReader(Convert.ToString(openFileDialog1.FileName)); while (!sr.EndOfStream) { string l= sr.ReadLine(); string[] parsedLine = l.Split(new char[] { '=' },StringSplitOptions.RemoveEmptyEntries); // Not working: if (parsedLine.Length == 0) { MessageBox.Show("Ignoring line"); }
Any help would be great!
-
dtsg almost 12 yearsCould you not just do
if(l.contains("/0"){l.replace("/0","");}
-
yogi almost 12 yearsWhat is
line
doing there you haven't defined it anywhere is itl
?? -
Daniel Flannery almost 12 yearssorry.
line
should have beenl
. typo. -
Daniel Flannery almost 12 years@Duane. This does not work as
'\0
is seen as a null character, not a series of character "\0" -
dtsg almost 12 years@L337BEAN Yep, wasn't 100% sure so didn't post it as an answer
-
-
bruno conde almost 12 yearsThe line to ignore can contain other characters.
-
bruno conde almost 12 yearsThe line to ignore can contain other characters.
-
Daniel Flannery almost 12 yearsDoes not work. Operator "==" cannot be applied to operands of type 'string' and 'char'
-
Daniel Flannery almost 12 yearsThis is not working.
string does not contain a definition for 'isEmpty'
Am I missing an assembly reference or something ? -
Me.Name almost 12 years@L337BEAN Oops, sorry, didn't read correctly, thought parsedLine was a read in string, added a linq alternative for the string array.
-
dtsg almost 12 yearsHe probably means
string.IsNullOrEmpty("string")
-
xdazz almost 12 years@L337BEAN I missed, in C#,
l == ""
is ok for testing empty string. -
Daniel Flannery almost 12 yearsThis is working to a certain extent. The problem now is that It is removing everything from the string each time. For example if
l
containsH/0e/0l/0l/0o/0
after your line of code its passing l to parsedLine as""
. I would like to keep thehello
-
Daniel Flannery almost 12 yearshmm, this may be due to a space in the logfile. Ill check on it.
-
Daniel Flannery almost 12 yearsThe logfile is riddles with null characters. The programs is picking up spaces as them and saving them null characters. Only way I can see to get around this is to check if the first character of l is null and the second character is
=
-
Me.Name almost 12 years@L337BEAN So it's more a matter of removing all occurences of \0 than to remove entries where only a \0 is entered? In that case you could use l = l.Replace("\0", " ") to replace with spaces or l = l.Replace("\0", ""); to remove them altogether. Would that be viable?
-
Security Hound almost 12 years@brunocondeq - What characters would those be? Based on the MSDN documentation it would ignore a null character and an empty string.
-
Daniel Flannery almost 12 yearsWell I want to remove empty lines ( which I can do ), and lines thats first character is
\0
and second character=
. These are being used as breaks in the logfile. eg:= = = = = = = =
is being read as=\0=\0=\0=\0=\0=\0=\0=\0
-
Me.Name almost 12 years@L337BEAN For that example, the 2nd code sample should work, but I think the replace mentioned before still is easiest: string l= sr.ReadLine().Replace("\0",""); For the last example that would create a lot of empty elements after the split, but they would be removed with the RemoveEmtpyEntries clause. If the replace is not a viable solution, could you post (or upload) a longer sample file with the different scenarios?
-
Daniel Flannery almost 12 yearsThe trouble is that
line.Replace("\0", "");
is not working. printing the string after running this still shows the\0
as spaces. -
Daniel Flannery almost 12 yearsWait. I believe im using it wrong. Update: yes, your correct. this is working.