gpt4 book ai didi

C# 检测字符串数组中的空字符

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:56 27 4
gpt4 key购买 nike

我正在使用 StreamReader 从文本文件读入字符串数组 text[]。文本文件中的一行在数组的位置 1 -> 20 中被读取为 "\0"。我将如何去检测这个空字符并忽略这一行。

代码示例:

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");
}

任何帮助都会很棒!

最佳答案

假设你的意思是一个带有 ascii 码的字符:0

   if (parsedLine.Length == 0 || parsedLine[0] == '\0')continue;

编辑如果 parsedLine 是一个字符串,上面的方法将起作用,但对于您的代码中的解析:

    string[] parsedLine = l.Split(new char[] { '=' },StringSplitOptions.RemoveEmptyEntries)
.Where(s=>s.Length != 1 || s[0] != '\0').ToArray();

关于C# 检测字符串数组中的空字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11135908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com