修剪不处理空字符

时间:2015-09-17 06:51:30

标签: c# .net sql-server

我对trim方法有一个非常奇怪的问题。我试图修剪从数据库收到的字符串。这是我目前的方法:

string debug = row["PLC_ADDR1_RESULT"].ToString();
SPCFileLog.WriteToLog(String.Format("Debug: ${0}${1}",debug,Environment.NewLine));
debug = debug.Trim();
SPCFileLog.WriteToLog(String.Format("Debug2: ${0}${1}", debug, Environment.NewLine));
debug = debug.Replace(" ", "");
SPCFileLog.WriteToLog(String.Format("Debug3: ${0}${1}", debug, Environment.NewLine));

产生如下文件输出:

Debug: $    $
Debug2: $    $
Debug3: $    $ 

检查文件中的十六进制代码显示了一些有趣的内容。假设为空的空格不是十六进制20(空格),但是它们被设置为00(空?)

enter image description here

我们的数据库如何包含这样的数据是另一个谜,但无论如何,我需要修剪那些无效的(?)空字符。我怎么能这样做?

3 个答案:

答案 0 :(得分:6)

如果您只想从字符串中删除所有空字符,请尝试以下操作:

debug = debug.Replace("\0", string.Empty);

如果您只想从字符串的末尾删除它们:

debug = debug.Trim('\0');

空字符没有什么特别之处,但它们不被视为空格。

答案 1 :(得分:5)

String.Trim()只是不认为NUL字符(\0)是空格。最终,它调用this函数来确定空格,而不是将其视为空格。

坦率地说,我认为这是有道理的。通常\0不是空格。

答案 2 :(得分:0)

@Will Vousden会让我走上正轨... https://stackoverflow.com/a/32624301/12157575

-但是,我没有尝试重写或删除行,而是在命中以linq语句中的控制字符开头的StreamReader / StreamWriter之前过滤掉了行:

string ctrlChar = "\0"; // "NUL" in notepad++  

// linq statement: "where"
!line.StartsWith(ctrlChar)  
// could also easily do "Contains" instead of "StartsWith"

更多内容:

internal class Program
{
    private static void Main(string[] args)
    {
        // dbl space writelines
        Out.NewLine = "\r\n\r\n";

        WriteLine("Starting Parse Mode...");

        string inputFilePath = @"C:\_logs\_input";
        string outputFilePath = @"C:\_logs\_output\";
        string ouputFileName = @"consolidated_logs.txt";

        // chars starting lines we don't want to parse
        string hashtag = "#"; // logs notes
        string whtSpace = " "; // white space char
        string ctrlChar = "\0"; // "NUL" in notepad++


        try
        {
            var files =
                from file in Directory.EnumerateFiles(inputFilePath, "*.log", SearchOption.TopDirectoryOnly)
                from line in File.ReadLines(file)
                where !line.StartsWith(hashtag) &&
                !line.StartsWith(whtSpace) &&
                line != null &&
                !string.IsNullOrWhiteSpace(line) &&
                !line.StartsWith(ctrlChar) // CTRL CHAR FILTER
                select new
                {
                    File = file,
                    Line = line
                };

            using (StreamWriter writer = new StreamWriter(outputFilePath + ouputFileName, true))
            {
                foreach (var f in files)
                {
                    writer.WriteLine($"{f.File},{f.Line}");

                    WriteLine($"{f.File},{f.Line}"); // see console
                }

                WriteLine($"{files.Count()} lines found."); 
                ReadLine(); // keep console open
            }
        }
        catch (UnauthorizedAccessException uAEx)
        {
            Console.WriteLine(uAEx.Message);
        }
        catch (PathTooLongException pathEx)
        {
            Console.WriteLine(pathEx.Message);
        }
    }
}