[英]Use split to remove parts of a string that are surrounded by curly double quotes
我将以下内容用作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有效的。 我本来确实包含直(非卷曲)双引号,但是它们并不重要,并且确实存在一些问题,因为它们没有前后分隔符版本。
这是一个小的可验证示例,在您的代码中还有其他优点:
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"
因此,您想删除评论 ,例如
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);
只需将双引号放在单引号之间,而不使用转义符:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.