Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 是否删除富文本框的最后一行?_C#_Regex_Substring_Stringbuilder - Fatal编程技术网

C# 是否删除富文本框的最后一行?

C# 是否删除富文本框的最后一行?,c#,regex,substring,stringbuilder,C#,Regex,Substring,Stringbuilder,我想删除richtextbox的最后一行,它以结尾;半列。我想删除这一行直到;位于最后一个半列之前的半列 示例: hello do not delete this line; hello this sentence will continue... untill here; hello do not delete this line; private void button1_Click_1(object sender, EventArgs e) { List<stri

我想删除richtextbox的最后一行,它以结尾;半列。我想删除这一行直到;位于最后一个半列之前的半列

示例:

hello do not delete this line;
hello this sentence will continue...
untill here;
hello do not delete this line;
private void button1_Click_1(object sender, EventArgs e) {
        List<string> myList = richTextBox1.Lines.ToList();
        if (myList.Count > 0) {
            myList.RemoveAt(myList.Count - 1);
            richTextBox1.Lines = myList.ToArray();
            richTextBox1.Refresh();
        }
    }
结果应为:

hello do not delete this line;
hello this sentence will continue...
untill here;
hello do not delete this line;
private void button1_Click_1(object sender, EventArgs e) {
        List<string> myList = richTextBox1.Lines.ToList();
        if (myList.Count > 0) {
            myList.RemoveAt(myList.Count - 1);
            richTextBox1.Lines = myList.ToArray();
            richTextBox1.Refresh();
        }
    }
我的代码:

hello do not delete this line;
hello this sentence will continue...
untill here;
hello do not delete this line;
private void button1_Click_1(object sender, EventArgs e) {
        List<string> myList = richTextBox1.Lines.ToList();
        if (myList.Count > 0) {
            myList.RemoveAt(myList.Count - 1);
            richTextBox1.Lines = myList.ToArray();
            richTextBox1.Refresh();
        }
    }
private void按钮1\u单击1(对象发送者,事件参数e){
List myList=richTextBox1.Lines.ToList();
如果(myList.Count>0){
myList.RemoveAt(myList.Count-1);
richTextBox1.Lines=myList.ToArray();
richTextBox1.Refresh();
}
}

我不确定富文本框是如何工作的,但类似于

input = {rich text box text}
int index = text.lastIndexOf(";");
if (index > 0) 
{
    input = input.Substring(0, index);
}

// put input back in text box

我不确定富文本框是如何工作的,但类似于

input = {rich text box text}
int index = text.lastIndexOf(";");
if (index > 0) 
{
    input = input.Substring(0, index);
}

// put input back in text box
使用以下命令:

var last = richTextBox1.Text.LastIndexOf(";");
if (last > 0)
{
   richTextBox1.Text = richTextBox1.Text.Substring(0, last - 1);
   var beforelast = richTextBox1.Text.LastIndexOf(";");
   richTextBox1.Text = richTextBox1.Text.Substring(0, beforelast + 1);
}
else
{
   richTextBox1.Text = "";
}
您没有指定其他场景(即,当字符串不包含“;”时) 此代码删除从“;”开始的字符串,该字符串位于“最后一个”;“到最后一个”;”。 它删除最后一个分号和其后的文本,然后查找新的最后一个“;”。最后删除此“;”之后的文本使用以下命令:

var last = richTextBox1.Text.LastIndexOf(";");
if (last > 0)
{
   richTextBox1.Text = richTextBox1.Text.Substring(0, last - 1);
   var beforelast = richTextBox1.Text.LastIndexOf(";");
   richTextBox1.Text = richTextBox1.Text.Substring(0, beforelast + 1);
}
else
{
   richTextBox1.Text = "";
}
您没有指定其他场景(即,当字符串不包含“;”时) 此代码删除从“;”开始的字符串,该字符串位于“最后一个”;“到最后一个”;”。 它删除最后一个分号和其后的文本,然后查找新的最后一个“;”。最后删除此“;”

找到解决方案后的文本:

找到解决方案:

怎么样

string input = "your complete string; Containing two sentences";

List<string> sentences = s.Split(';').ToList();

//Delete the last sentence
sentences.Remove(sentences[sentences.Count - 1]);

string result = string.Join(" ", sentences.ToArray());
string input=“您的完整字符串;包含两个句子”;
列出句子=s.Split(“;”).ToList();
//删除最后一句
删除(句子[句子数-1]);
字符串结果=string.Join(“,句子.ToArray());
怎么样

string input = "your complete string; Containing two sentences";

List<string> sentences = s.Split(';').ToList();

//Delete the last sentence
sentences.Remove(sentences[sentences.Count - 1]);

string result = string.Join(" ", sentences.ToArray());
string input=“您的完整字符串;包含两个句子”;
列出句子=s.Split(“;”).ToList();
//删除最后一句
删除(句子[句子数-1]);
字符串结果=string.Join(“,句子.ToArray());

对于那些在这么多年后发现这个问题的人

使用.Text属性或.Lines属性的解决方案最终会从现有文本中删除格式。相反,请使用类似以下内容来保留格式:

var i = textBox.Text.LastIndexOf("\n");
textBox.SelectionStart = i;
textBox.SelectionLength = o.TextLength - i + 1;
textBox.SelectedText = "";
请注意,如果文本框处于只读模式,则无法修改SelectedText。在这种情况下,您需要如下设置和重置只读:

textBox.ReadOnly = false;
textBox.SelectedText = "";
textBox.ReadOnly = true;

对于那些在这么多年后发现这个问题的人

使用.Text属性或.Lines属性的解决方案最终会从现有文本中删除格式。相反,请使用类似以下内容来保留格式:

var i = textBox.Text.LastIndexOf("\n");
textBox.SelectionStart = i;
textBox.SelectionLength = o.TextLength - i + 1;
textBox.SelectedText = "";
请注意,如果文本框处于只读模式,则无法修改SelectedText。在这种情况下,您需要如下设置和重置只读:

textBox.ReadOnly = false;
textBox.SelectedText = "";
textBox.ReadOnly = true;

这条线在干什么?input={rich text box text}抱歉,这是伪代码。从文本框中获取文本。看:这条线在干什么?input={rich text box text}抱歉,这是伪代码。从文本框中获取文本。请参阅:这将从现有文本中删除格式。而是使用SelectedText方法。这将从现有文本中删除格式。而是使用SelectedText方法。