我使用以下内容作为C#中的split
函数的参数:
private char[] delimiterComment = { '(', '{', '[', '\u201C' };
private char[] delimiterEndComment = { ')', '}', ']', '\u201D' };
适用于所有"括号"但不是"卷曲的双引号"。我不明白为什么。它是split
中的错误,还是卷曲引号字符的一个特征?
我有一个输入字符串,如: “这是预先评论”然后一些重要的信息[可能有一个嵌入的评论],然后一些更重要的信息(然后有一个帖子评论)
我希望删除评论,但是在结构中捕获它们,留下一个干净的信息字符串。这一切都用括号精美地工作,直到我试图添加卷曲双引号作为额外的分隔符... (我知道嵌入式评论是作为帖子评论故意收集的) 我写的代码如下:
class CommentSplit
{
public bool split = false;
public bool error = false;
public string original = "";
public string remainder = "";
public string preComment = "";
public string postComment = "";
public CommentSplit(string inString, char[] startComment, char[] endComment, string[] ignoreStrings, string[] addStrings, bool usePre) // creator
{
if (inString == null)
return;
original = inString;
string[] starts = inString.Split(startComment);
if (starts.Length == 1)
{
remainder = inString;
return;
}
if (starts[0] != "")
remainder += starts[0].TrimEnd();
for (int i = 1; i < starts.Length; i++)
{
string[] ends = starts[i].Split(endComment);
if (ends.Length != 2) // more than one end comment for a start comment - BUT what about one start and one end comment
{
error = true;
return;
}
if (addStrings == null)
{
if (ignoreStrings == null)
{
if ((remainder == "") && usePre)
preComment += ends[0];
else
postComment += ends[0];
}
else
{
bool ignore = false;
for (int z = 0; z < ignoreStrings.Length; z++)
{
if (ends[0].ToLower() == ignoreStrings[z])
ignore = true;
}
if (!ignore) // was a comment but we might want to ignore it
{
if ((remainder == "") && usePre)
{
if (preComment != "")
preComment += " ";
preComment += ends[0];
}
else
{
if (postComment != "")
postComment += " ";
postComment += ends[0];
}
}
}
}
else
{
bool add = false;
for (int z = 0; z < addStrings.Length; z++)
{
if (ends[0].ToLower() == addStrings[z])
add = true;
}
if (add) // was a comment but want it in the remainder
{
if (remainder != "")
remainder += " ";
remainder += ends[0];
}
else
{
if (ignoreStrings == null)
{
if ((remainder == "") && usePre)
preComment += ends[0];
else
postComment += ends[0];
}
else
{
bool ignore = false;
for (int z = 0; z < ignoreStrings.Length; z++)
{
if (ends[0].ToLower() == ignoreStrings[z])
ignore = true;
}
if (!ignore) // was a comment but we might want to ignore it
{
if ((remainder == "") && usePre)
{
if (preComment != "")
preComment += " ";
preComment += ends[0];
}
else
{
if (postComment != "")
postComment += " ";
postComment += ends[0];
}
}
}
}
}
if (remainder != "")
remainder += " ";
remainder += ends[1].Trim();
}
split = true;
} // CommentSplit
}
我应该注意到我是一个退休的C程序员,涉及C#,所以我的风格可能不是OOP效率。我最初做的包括直(非卷曲)双引号,但它们并不重要,事实上还没有内容,因为它们没有前后分隔符版本。
答案 0 :(得分:0)
它在代码中的其他内容,因为这个小的可验证示例正常工作:
char[] delimiterComment = { '(', '{', '[', '\u201C', '\u201D', '"', '“', '”', '}', ']', ')' };
string stringWithComment = "this has a “COMMENT” yeah really";
var result = stringWithComment.Split(delimiterComment);
//Output:
//result[0] = "this has a "
//result[1] = "COMMENT"
//result[2] = " yeah really"
答案 1 :(得分:0)
所以你想要切断评论,例如
123 (456 [789) abc ] -> 123 abc ]
在这种情况下,您可以尝试一个简单的循环:
//TODO: I suggest combining starts and ends into array of pairs, e.g.
// KeyValuePair<string,string>[]
private static string CutOffComments(string source, char[] starts, char[] ends) {
if (string.IsNullOrEmpty(source))
return source;
StringBuilder sb = new StringBuilder(source.Length);
int commentIndex = -1;
foreach (var c in source) {
if (commentIndex >= 0) { // within a comment, looking for its end
if (c == ends[commentIndex])
commentIndex = -1;
}
else { // out of comment, do we starting a new one?
commentIndex = Array.IndexOf(starts, c);
if (commentIndex < 0)
sb.Append(c);
}
}
//TODO:
// if (commentIndex >= 0) // dungling comment, e.g. 123[456
return sb.ToString();
}
用法:
string source = "123[456]789";
// 123789
string result = CutOffComments(source, delimiterComment, delimiterEndComment);
答案 2 :(得分:0)
只需在单引号之间加上双引号而不使用转义符:
private char[] delimiterComment = { '(', '{', '[', '\u201C', '"' };
private char[] delimiterEndComment = { ')', '}', ']', '\u201D', '"' };
输入:
string s = "abc(121), {12}, \" HI \"";
Console.WriteLine(string.Join(Environment.NewLine,(s.Split(delimiterComment)).Select(s=> s)));
输出
abc
121),
12},
HI