如果我有一个字符串:
但是如果我有一个字符串:
希望这能让您明白。
str1|str2|str3|srt4
,并使用|
作为分隔符进行解析。我的输出将是str1 str2 str3 str4
。但是如果我有一个字符串:
str1||str3|str4
,输出将是str1 str3 str4
。我希望我的输出结果像这样:str1 null/blank str3 str4
。希望这能让您明白。
string createText = "srt1||str3|str4";
string[] txt = createText.Split(new[] { '|', ',' },
StringSplitOptions.RemoveEmptyEntries);
if (File.Exists(path))
{
//Console.WriteLine("{0} already exists.", path);
File.Delete(path);
// write to file.
using (StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode))
{
sw.WriteLine("str1:{0}",txt[0]);
sw.WriteLine("str2:{0}",txt[1]);
sw.WriteLine("str3:{0}",txt[2]);
sw.WriteLine("str4:{0}",txt[3]);
}
}
输出
str1:str1
str2:str3
str3:str4
str4:"blank"
那不是我要找的。这是我想编写的代码:
str1:str1
str2:"blank"
str3:str3
str4:str4