简体   繁体   English

C#从/ ** /之间的字符串中删除字符

[英]C# Remove characters from string between /**/

In string will be something like this: 在字符串中将是这样的:

for(I=1;I<10;I++) /*Else*/ {A = "for"; B = 'c'; break;} // while(a < 10)

I would like to remove from this string anything that is between /**/ and between "" and between '' and anything after // 我想从这个字符串任何介于删除/**/之间""之间''后什么//

Here is example 这是例子

input: 输入:

for(I=1;I<10;I++) /*Else*/ {A = "for"; B = 'c'; break;} // while(a < 10)

output: 输出:

FOR(i=1;i<10;i++) /**/ {a = ""; b = ''; BREAK;} // 

I know that I have to go through characters in string with: 我知道我必须使用以下字符串:

for (int i = 0; i < input.Length; i++)
{
    // search for /**/ ?
}

but i don't know how should I remove characters and put the other characters in new string. 但我不知道该如何删除字符并将其他字符放入新字符串中。

string sentence = "for(I=1;I<10;I++) /*Else*/ {A = "for"; B = 'c'; break;} // while(a < 10)";
        //how can I remove these characters from string so it will look something like this?
string shortSentence = "FOR(i=1;i<10;i++) /**/ {a = ""; b = ''; BREAK;} //";

Try this. 尝试这个。 The idea is to first remove the stuff at the end, the // comments. 这个想法是首先删除最后的内容,即//注释。 Then afterwards, we search for /* and the next /, remove that text (we are left with /**/ , save the index of that string and remove it as well. Once we have no more / strings, we reinsert the /**/ into the positions we removed them from. 然后,我们搜索/ *和下一个/,删除该文本(剩下/**/ ,保存该字符串的索引,并将其也删除。一旦没有更多/字符串,我们就重新插入/**/进入我们从中删除的职位。

NOTE: This won't work if you have something like this /* comment /* more comment */ and some other part */ . 注意:如果您有类似/* comment /* more comment */ and some other part */则此方法将无效。 But this also isn't a valid comment in C#, which is the language you tagged here. 但这在C#中也不是有效的注释,这是您在此处标记的语言。

 static string s = "for(int i = 0; i < 10; i++){ var myVar = \"Test\"; /*commented out code*/ Console.WriteLine(\"stuff\"); /* more comments here*/}//my comments here \n var myOverVar = /*more stuff removed*/ true; //some more comments";


static void Main(string[] args)
{
    //s.IndexOf()
    var result = new List<string>();
    var lines = s.Split(new[] {"\n"}, StringSplitOptions.RemoveEmptyEntries); //use this if you have multiple code lines, separated by new lines.
    foreach (var line in lines)
    {
        var listOfPositions = new List<int>();
        var l = line;
        //chop off everything after comments
        var indexOfLineComment = l.IndexOf("//");

        l = l.Remove(indexOfLineComment + 2); // 2 because // is two characters long
                var openBraceIndex = l.IndexOf("/*");
        while (openBraceIndex != -1) //-1 indicates that we didn't find /*
        {
            var closingBraceIndex = l.IndexOf("*/");
            if (closingBraceIndex == -1)
            {
                break; //you didn't specify how to the case when an error in syntax was made, but handle it here
            }
            l = l.Remove(openBraceIndex + 2, closingBraceIndex - openBraceIndex-2);
            var ind = l.IndexOf("/**/");
            listOfPositions.Insert(0, ind);
            l = l.Remove(ind, 4);
            openBraceIndex = l.IndexOf("/*");

        }
        foreach (var i in listOfPositions)
            if (l.Length <= i)
                l = l + "/**/";
            else
                l = l.Insert(i, "/**/");
        result.Add(l);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM