Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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# 删除word文档中的行,直到C中出现字符串#_C#_String - Fatal编程技术网

C# 删除word文档中的行,直到C中出现字符串#

C# 删除word文档中的行,直到C中出现字符串#,c#,string,C#,String,我正在尝试一个案例,我需要找到一个字符串并删除所有行,直到找到该字符串。 我有一个像这样的医生 ABC。。 DEF。。 指数 LKV MNP 所以,我想删除每一行,直到找到单词index,包括index和reserve line 我尝试了以下代码,但它看起来不工作,并引发了一些异常 if (this.Doc_path.Text != "") { string Doc_pth = this.Doc_path.Text;

我正在尝试一个案例,我需要找到一个字符串并删除所有行,直到找到该字符串。 我有一个像这样的医生 ABC。。 DEF。。 指数 LKV MNP 所以,我想删除每一行,直到找到单词index,包括index和reserve line

我尝试了以下代码,但它看起来不工作,并引发了一些异常

    if (this.Doc_path.Text != "")
        {
            string Doc_pth = this.Doc_path.Text;
            string Serch_String1 = "Appendix";
            Word.Application app = new Word.Application();
            app.Visible = true;
            app.WindowState = Word.WdWindowState.wdWindowStateNormal;

            Word.Document docOpen = app.Documents.Open(Doc_pth);
            Object missing = System.Reflection.Missing.Value;
            docOpen.Content.Find.ClearFormatting();
            try
            {
                var range = docOpen.Content;
                foreach (var para in range.Paragraphs)
                {
                    //if (.Find.Execute(Serch_String1))
                    //if (range.Find.Execute(Serch_String1))
                    if(para.ToString().Contains(Serch_String1))
                    {
                        MessageBox.Show("found");
                        break;
                        
                    }
                    else
                    {
                        //range.Expand(Word.WdUnits.wdParagraph); // or change to .wdSentence or .wdParagraph
                        ///range.Expand(Word.WdUnits.wdParagraph); // or change to .wdSentence or .wdParagraph
                        range.Delete(para);
                    }
                    
                }
            }
            catch(Exception exe)
            {
                MessageBox.Show(exe.ToString());
                throw;
            }
            MessageBox.Show("Done!");
            docOpen.Close(ref missing, ref missing, ref missing);
            app.Quit(ref missing, ref missing, ref missing);

            //mp.Close();

        }
        else
        {
            MessageBox.Show("Enter valid path for document");
        }
    }
}

以下过程可能有助于从查找到字符串的行中删除文本

我以您提供的字符串为例

//let the original text
string strDel = "ABC.. DEF.. index LKV MNP";

//the word to upto delete the text
string delTillWord = "index";

//length of the word upto delete
int delUpto = delTillWord.Length;

//first remove the string upto first letter of upto deleting word
string strAfterDel = strDel.Remove(0, (strDel.Length - strDel.IndexOf(delTillWord)));

//then delete remaining word by taking its length
strAfterDel = strAfterDel.Remove(0, delUpto);
在变量
strAfterDel
中,剩余的字符串是
“LKV MNP”

另一种方法是创建一个单独的方法并调用它:

public static string GetAfterDelete(string strDel, string delTillWord)
{
    //length of the word upto delete
    int delUpto = delTillWord.Length;

    //first remove the string upto first letter of upto deleting word
    string strAfterDel = strDel.Remove(0, (strDel.Length - strDel.IndexOf(delTillWord)));

    //then delete remaining word by taking its length
    strAfterDel = strAfterDel.Remove(0, delUpto);

    return strAfterDel;
}
使用上述方法:

string strDel = "ABC.. DEF.. index LKV MNP";
string strAfterDel = GetAfterDelete(strDel, "index");

使用contains将只返回布尔值。可以将indexOf()与string.remove()结合使用

不确定
para
对象的上下文,因此可能不需要toString()

但要获取术语/字符的索引位置,可以执行以下操作:
int position=para.indexOf('term')
然后使用以下方法删除术语前的文本:
string result=para.Remove(0,位置)

其中两个参数是起始位置和结束位置。

它会引发哪些异常?请详细说明。@Amit欢迎使用StackOverflow!如果您有一个(或多个)抛出异常的示例,请使用这些异常编辑您的帖子,或者将它们包含在评论中。这将有助于社区更快地解决你的问题。很高兴你打败了我。目的不是打败任何人:)那么你必须为一个好的姿态投票,这也是一种很好的文化:)谢谢你提供的信息。我在文档上执行这些操作。我需要在文档中搜索一个字符串,该字符串位于一个向下翻页的位置,需要删除它之前的所有内容,包括搜索的字符串。