下面是我使用的示例代码.净核心6:
using System;
namespace testTerminator
{
class Program
{
static void Main(string[] args)
{
// Given an array of bytes:
byte[] array = {72, 101, 108, 108, 111, 0, 0, 32, 72, 101};
// Converted to a string:
string data = System.Text.Encoding.ASCII.GetString(array);
// Why does the following line not return the position
// of the null characters?
int terminator = data.IndexOf("\0\0"); // returns 0
// Output:
Console.WriteLine("Data is '{0}'", data);
Console.WriteLine("Found terminator at {0}", terminator);
// Verify null characters are still in the data string:
byte[] dataBytes = System.Text.Encoding.ASCII.GetBytes(data);
for (int i = 0; i<dataBytes.Length; i++){
Console.Write("{0},", dataBytes[i]);
}
Console.WriteLine();
}
}
}
这将产生以下输出:
Data is 'Hello He'
Found terminator at 0
72,101,108,108,111,0,0,32,72,101,
当空字符位于位置6时,为什么IndexOf("\0\0")返回零?