Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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#_.net - Fatal编程技术网

C# 如何拆分特殊字符'##';和';}';

C# 如何拆分特殊字符'##';和';}';,c#,.net,C#,.net,我已经试过了 还是不走运 正文1: 一些包含}和}或}的文本 文本2: 一些包含###和###的文本,或者## 这是我的密码 string str1 = "Some text that contained } and again } or maybe }"; // Some time its contained ## string[] words; if (str1.Contains("}")) { words = str1.Split("}"); } else if (str1.Co

我已经试过了 还是不走运

正文1:

一些包含}和}或}的文本

文本2:

一些包含###和###的文本,或者##

这是我的密码

string str1 = "Some text that contained } and again } or maybe }";
// Some time its contained ##

string[] words;
if (str1.Contains("}"))
{
    words = str1.Split("}");
}
else if (str1.Contains ("##"))
{
    words = str1.Split("##");
} else {
    words = null;
}
我犯了两个错误

与“string.Split(params char[])匹配的最佳重载方法具有一些无效参数

参数“1”:无法从“字符串”转换为“字符[]”

尝试使用

str1.Split(new [] {"}"}, StringSplitOptions.RemoveEmptyEntries);

如果要保留空字符串,请使用
StringSplitOptions。无

仅在下一个签名中将输入作为字符串:
Split(String[],StringSplitOptions)
Split(String[],Int32,StringSplitOptions)
。所以至少您需要指定
StringSplitOptions
并将一个字符串转换为一个字符串的数组,否则编译器不知道您试图调用什么方法

您可以通过删除一条
if
语句来减少逻辑<代码>拆分
方法如果没有发现输入字符串的出现,则不会引发任何异常

string str1 = "Some text that contained } and again } or maybe }";

string[] words;
if (str1.Contains("}") || str1.Contains ("##"))
{
    words = str1.Split(new [] {"}", "##"}, StringSplitOptions.RemoveEmptyEntries);

}
else
{
    words = null;
}

正如Dave提到的,字符串拆分只接受一个字符。如果需要拆分字符串,请使用以下代码

string str1 = "Some text that contained } and again } or maybe }";
    // Some time its contained ##

string[] words;
if (str1.Contains("}"))
{
    words = str1.Split(new string[] { "}" }, StringSplitOptions.None);
}
else if (str1.Contains ("##"))
{
    words = str1.Split(new string[] { "##" }, StringSplitOptions.None);
} else {
    words = null;
}

如果需要匹配
“##”


试试这个代码。请注意,您也可以使用
Regex
,因此该类允许您按模式拆分:

string str1 = "Some text that contained } and again } or maybe }";
// Some time its contained ##

string[] words;
if (str1.Contains("}"))
{
    words = str1.Split('}');
}
else if (str1.Contains ("##"))
{
    words = Regex.Split(str1, @"\#\#");
} else {
    words = null;
}
按照C#中的工作方式,传入的参数应该是字符数组或带有选项的字符串。该方法还有其他重载,但这些重载与您的问题无关。
与其使用
words=str1.Split(“}”)
,不如使用
words=str1.Split(“}”)
,它传入一个字符,而不是字符串作为参数。
对于需要检查字符串而不是字符的情况,应该使用
words=str1.Split(新字符串[]{“##”},StringSplitOptions.None)
而不是
words=str1.Split(“###”)

您的最终代码应该如下所示

string str1 = "Some text that contained } and again } or maybe }";
        // Some time its contained ##

string[] words;
if (str1.Contains("}"))
{
    words = str1.Split( ('}'));
}
else if (str1.Contains("##"))
{
    words = str1.Split(new string[] { "##" }, StringSplitOptions.None);
}
else
{
    words = null;
}

查看有关如何使用拆分方法的教程

如果要按}或#拆分两个字符串,可以使用要拆分的字符串数组

stringToSplit.Split(new []{"}","##"}, StringSplitOptions.None);

要看到这幅作品,请看一下这幅涂鸦

为什么不在一行字符串中完成呢

str1 = "Some text that contained } and again } or maybe }"; 
var items = str1.Split(new string[] { "##" ,"}" },StringSplitOptions.RemoveEmptyEntries);

双引号表示字符串。单引号表示字符。Radiaku您熟悉Split()方法吗。。?同样根据您的示例,代码将永远不会拆分(“###”)该字符串中的哪里有##介意解释一下吗?我还在学习C#(autodidact)@radiaku,我已经添加了一点解释来回答。为什么不在一行
string str1=“Some text that contain}和}或者}”中完成这一切呢;var items=str1.Split(新字符串[]{“##”,“}”},StringSplitOptions.RemoveEmptyEntries)@DJKRAZE非常好的观点。我会更新我的答案。谢谢
string str1 = "Some text that contained } and again } or maybe }";
        // Some time its contained ##

string[] words;
if (str1.Contains("}"))
{
    words = str1.Split( ('}'));
}
else if (str1.Contains("##"))
{
    words = str1.Split(new string[] { "##" }, StringSplitOptions.None);
}
else
{
    words = null;
}
stringToSplit.Split(new []{"}","##"}, StringSplitOptions.None);
str1 = "Some text that contained } and again } or maybe }"; 
var items = str1.Split(new string[] { "##" ,"}" },StringSplitOptions.RemoveEmptyEntries);