Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Char[]的子序列是否等于String的内容?_C# - Fatal编程技术网

C# Char[]的子序列是否等于String的内容?

C# Char[]的子序列是否等于String的内容?,c#,C#,有没有更快捷的方法来检查来自特定索引的字符数组的子序列是否等于字符串的子序列 bool Matches (Char[] cs, int i, string s) { return cs.Skip(i).Take(s.Length).SequenceEqual(s); } 假设cs和s从不null 和运行时一样快。 我也可以不创建字符串的新实例吗?因为两者都可以看作是字符数组 我希望能有一些类似于的东西,只需使用一个简单的for循环即可。此选项旨在消除s上的边界检查 bool Match

有没有更快捷的方法来检查来自特定索引的字符数组的子序列是否等于字符串的子序列

bool Matches (Char[] cs, int i, string s)
{
    return cs.Skip(i).Take(s.Length).SequenceEqual(s);
}
假设
cs
s
从不
null

和运行时一样快。 我也可以不创建字符串的新实例吗?因为两者都可以看作是字符数组


我希望能有一些类似于

的东西,只需使用一个简单的for循环即可。此选项旨在消除
s
上的边界检查

bool Matches (char[] chars, int offset, string s)
{
    if(offset < 0)
        throw new ArgumentOutOfRangeException("offset");
    if(chars.Length - offset < s.Length)
        throw new ArgumentException();
    for(int i = 0; i < s.Length; i++)
    {
        if(chars[offset + i] != s[i])
            return false;
    }
    return true;
}
bool匹配(char[]chars,int offset,string s)
{
如果(偏移量<0)
抛出新ArgumentOutOfRangeException(“偏移量”);
if(字符长度-偏移量<字符长度)
抛出新ArgumentException();
对于(int i=0;i
只需对循环使用一个简单的
。此选项旨在消除
s
上的边界检查

bool Matches (char[] chars, int offset, string s)
{
    if(offset < 0)
        throw new ArgumentOutOfRangeException("offset");
    if(chars.Length - offset < s.Length)
        throw new ArgumentException();
    for(int i = 0; i < s.Length; i++)
    {
        if(chars[offset + i] != s[i])
            return false;
    }
    return true;
}
bool匹配(char[]chars,int offset,string s)
{
如果(偏移量<0)
抛出新ArgumentOutOfRangeException(“偏移量”);
if(字符长度-偏移量<字符长度)
抛出新ArgumentException();
对于(int i=0;i
好吧,您可以自己编写循环并删除枚举数:

if (cs == null || s == null)
    throw new ArgumentNullException();
if (i < 0 || i > cs.Length)
    throw new ArgumentException("i");
if (cs.Length - i != s.Length)
    return false;
for (int j = 0; j != s.Length; ++j) {
    if (s[j] != cs[j + i])
        return false;
}
return true;
if(cs==null | | s==null)
抛出新ArgumentNullException();
if(i<0 | | i>cs.长度)
抛出新的异常(“i”);
如果(cs.Length-i!=s.Length)
返回false;
对于(int j=0;j!=s.长度;++j){
如果(s[j]!=cs[j+i])
返回false;
}
返回true;
但这仍然不如调用本机字符串函数那么快,因为它会对每个下标访问进行边界检查(通常这不是什么大问题,但既然您追求的是速度,为什么不全力以赴呢)。因此,我们可以降低抽象级别并使用指针:

if (cs == null || s == null)
    throw new ArgumentNullException();
if (i < 0 || i > cs.Length)
    throw new ArgumentException("i");
if (cs.Length - i != s.Length)
    return false;
unsafe {
     fixed (char* ps = s, pcs_ = cs) {
        char* pcs = pcs_ + i;
        for (int j = 0; j != s.Length; ++j) {
            if (pcs[j] != ps[j])
                return false;
        }
    }
}
return true;
if(cs==null | | s==null)
抛出新ArgumentNullException();
if(i<0 | | i>cs.长度)
抛出新的异常(“i”);
如果(cs.Length-i!=s.Length)
返回false;
不安全{
固定(字符*ps=s,字符=cs){
char*pcs=pcs\uu+i;
对于(int j=0;j!=s.长度;++j){
如果(pcs[j]!=ps[j])
返回false;
}
}
}
返回true;

好吧,您可以自己编写循环并删除枚举数:

if (cs == null || s == null)
    throw new ArgumentNullException();
if (i < 0 || i > cs.Length)
    throw new ArgumentException("i");
if (cs.Length - i != s.Length)
    return false;
for (int j = 0; j != s.Length; ++j) {
    if (s[j] != cs[j + i])
        return false;
}
return true;
if(cs==null | | s==null)
抛出新ArgumentNullException();
if(i<0 | | i>cs.长度)
抛出新的异常(“i”);
如果(cs.Length-i!=s.Length)
返回false;
对于(int j=0;j!=s.长度;++j){
如果(s[j]!=cs[j+i])
返回false;
}
返回true;
但这仍然不如调用本机字符串函数那么快,因为它会对每个下标访问进行边界检查(通常这不是什么大问题,但既然您追求的是速度,为什么不全力以赴呢)。因此,我们可以降低抽象级别并使用指针:

if (cs == null || s == null)
    throw new ArgumentNullException();
if (i < 0 || i > cs.Length)
    throw new ArgumentException("i");
if (cs.Length - i != s.Length)
    return false;
unsafe {
     fixed (char* ps = s, pcs_ = cs) {
        char* pcs = pcs_ + i;
        for (int j = 0; j != s.Length; ++j) {
            if (pcs[j] != ps[j])
                return false;
        }
    }
}
return true;
if(cs==null | | s==null)
抛出新ArgumentNullException();
if(i<0 | | i>cs.长度)
抛出新的异常(“i”);
如果(cs.Length-i!=s.Length)
返回false;
不安全{
固定(字符*ps=s,字符=cs){
char*pcs=pcs\uu+i;
对于(int j=0;j!=s.长度;++j){
如果(pcs[j]!=ps[j])
返回false;
}
}
}
返回true;

新字符串(cs,i,s.Length)=s定义“更快”。跑得更快?可能不使用LINQ。写得更快?也许.@O.R.Mapper运行起来更快。这段代码读起来相当清楚。您是否有任何客观衡量的性能问题?@Jeroenvanevel它创建了3个枚举器。如果字符较大,则可能需要一些时间才能完成。
新字符串(cs,i,s.Length)=s定义“更快”。跑得更快?可能不使用LINQ。写得更快?也许.@O.R.Mapper运行起来更快。这段代码读起来相当清楚。您是否有任何客观衡量的性能问题?@Jeroenvanevel它创建了3个枚举器。如果字符比较大,则可能需要一些时间才能完成。我认为strncmp中的更多字符可能会比较慢,因为字符串的长度不必相等——它必须一直检查到第一个差异。大多数情况下,字符串相等性检查会在循环之前提前退出,因为字符串的长度通常只是不同,因此不相等。尽可能始终使用字符串相等。@AdamSpeight您可以调用C的
memcmp
,而不是
strncmp
。我想更多的strncmp可能会更慢,因为字符串的长度不必相等——它必须一直检查到第一个差异。大多数情况下,字符串相等性检查会在循环之前提前退出,因为字符串的长度通常只是不同,因此不相等。尽可能始终使用字符串相等。@AdamSpeight您可以调用C的
memcmp
,而不是
strncmp