C# split string with \n pay attention to the problem

Lionsure 2020-08-06 Original by the website

Usually Splite is used to separate some strings that are not particularly long, and the split character rarely uses the newline character \n, and there will be no unexpected problems. However, when using Splite to split the website logs with \n, unexpected problems appeared. For some reason, a space was added to the end of all lines except the last line.

 

If the website log to be analyzed is as follows:

2013-04-16 00:02:35 W3SVC7344 142.4.113.28 GET /abate/Abate.aspx type=afja&tn=%25e4%25ba%25a4%25e9%2580%259a%25e5%25ae%2589%25e5%2585%25a8%25e8%25ae%25be%25ef%25bf%25bd%2525b&an=542625&fg=1 80 - 66.249.74.137 Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) 200 0 0
2013-04-16 00:02:50 W3SVC7344 142.4.113.28 GET / - 80 - 222.77.187.33 Mozilla/5.0+(compatible;+MSIE+8.0;+Windows+NT+6.1;+Trident/5.0) 200 0 0
2013-04-16 00:02:52 W3SVC7344 142.4.113.28 GET /Abate/Abate.aspx type=fzxf&tn=%e6%a0%a1%e6%9c%8d&an=210112&fg=1 80 - 218.30.103.148 Sogou+web+spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07) 200 0 0
2013-04-16 00:05:58 W3SVC7344 142.4.113.28 GET /House/Item.aspx type=3&tf=&stf=&tn=&stn=&an=630105&fg=1 80 - 220.181.108.165Mozilla/5.0+(compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html) 200 0 0
2013-04-16 00:06:12 W3SVC7344 142.4.113.28 GET /at/201110/844885345.htm - 80 - 182.118.25.174 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+zh-CN;+rv:1.8.0.11)++Firefox/1.5.0.11;+360Spider 200 0 0
2013-04-16 04:30:07 W3SVC7344 142.4.113.28 GET /Abate/Abate.aspx an=430122&sf=4 80 - 61.135.249.221 Mozilla/5.0+(compatible;+YoudaoBot/1.0;+http://www.youdao.com/help/webmaster/spider/;+) 200 0 0
2013-04-16 04:30:13 W3SVC7344 142.4.113.28 GET /at/201207/535370620.htm - 80 - 123.151.148.178 Sosospider+(+http://help.soso.com/webspider.htm) 200 0 0

 

The analysis code is as follows:

private void ParseLogs()
       {
              StreamReader sr = new StreamReader(Server.MapPath("/db/an.log"));

       string lines = sr.ReadToEnd();//Read all logs into memory
              string[] al = lines.Split('\n');//Split log with \n

       for (int i = 0; i < al.Length; i++)
                     Response.Write("The last character per line=" + al[i].Substring(al[i].Length - 1) + "; Penultimate character=" + al[i].Substring(al[i].Length - 2, 1) + "<br />");

       sr.Close();
       }

 

Output result:

The last character per line= ; Penultimate character=0

The last character per line= ; Penultimate character=0

The last character per line= ; Penultimate character=0

The last character per line= ; Penultimate character=0

The last character per line= ; Penultimate character=0

The last character per line= ; Penultimate character=0

The last character per line=0; Penultimate character=

The above website logs all end with 0, and in the output result, only the "last character" of the last line is 0, and the last character of other lines has become a space, which is obviously added when splitting with a newline character \n.

If the programming involves the processing of the last character string, especially the last character string as a certain condition, this situation must not be ignored, otherwise the program cannot output the correct result.