Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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# 从C中的字符串中删除字符列表#_C#_String_Char - Fatal编程技术网

C# 从C中的字符串中删除字符列表#

C# 从C中的字符串中删除字符列表#,c#,string,char,C#,String,Char,我需要一个从字符串中删除字符列表的方法。我需要正确地支持所有角色,但我不确定我是否正确地管理代理角色 还有更好的办法吗 public static string Remove(string source, char[] oldChar) { if (string.IsNullOrEmpty(source) || oldChar == null || oldChar.Length == 0) return source; for (int i = source.I

我需要一个从字符串中删除字符列表的方法。我需要正确地支持所有角色,但我不确定我是否正确地管理代理角色

还有更好的办法吗

public static string Remove(string source, char[] oldChar)
{
    if (string.IsNullOrEmpty(source) || oldChar == null || oldChar.Length == 0)
        return source;

    for (int i = source.IndexOfAny(oldChar, 0); i != -1; i = source.IndexOfAny(oldChar, i))
        source = source.Remove(i, char.IsSurrogatePair(source, i) ? 2 : 1);

    return source;
}
多谢各位


Frank

以下内容不涉及代理项对,因为问题不清楚您想做什么。If满足从字符串中删除字符列表的要求

public static string Remove(string source, char[] oldChar)
{
    return String.Join("", source.ToCharArray().Where(a => !oldChar.Contains(a)).ToArray());
}
例如:

var s = "hello world";
var c = new[] { 'l', 'o' };
Remove(s, c); //returns: he wrd

如果您的目标是删除代理,我可以建议以下方法:

那么您希望如何支持代理对?因为您无论如何不能在单个
char
值中表示非BMP字符。。。也许您的第二个参数应该是
int[]
?请提供您期望的输入/输出示例。