简体   繁体   English

如果行以条件 C# 开头,则替换特定 position 中的字符

[英]Replace a character in specific position if line starts with condition C#

I am trying to modify a txt file, I need to change the 45 character with a P if the line starts with 8我正在尝试修改 txt 文件,如果行以 8 开头,我需要用 P 更改 45 字符

for (int i = 0; i < textBox.Lines.Length; i++)//Loops through each line of text in RichTextBox
           {

               string text = textBox.Lines[i];
               if ((text.Contains("8") == true)) //Checks if the line contains 8.
               {
                   char replace = 'P';
                   int startindex = textBox.GetFirstCharIndexFromLine(i);
                   int endindex = text.Length;
                   textBox.Select(startindex, endindex);//Selects the text.
                   richTextBox1.Text = textBox.Text.Substring(0, textBox.SelectionStart) + (0, textBox.Lines) + replace + textBox.Text.Substring(textBox.SelectionStart + 45);
     }}             

To accomplish your goal the code could be changed in this way为了实现您的目标,可以通过这种方式更改代码

//Loops through each line of text in RichTextBox
for (int i = 0; i < textBox.Lines.Length; i++)
{
    string text = textBox.Lines[i];
    //Checks if the line starts with "8".
    if (text.StartsWith("8")) 
    {
        // Find the 45th position from the start of the line
        int startindex = textBox.GetFirstCharIndexFromLine(i) + 45;
        // Starting from the found index select 1 char
        textBox.Select(startindex, 1);
        // Replace the selected char with the "P"
        textBox.SelectedText = "P";
    }
}

The key points changed are the way to select into a textbox.更改的关键点是将 select 放入文本框的方式。 The Select method requires a starting index and the number of character to select , finally, once you have a SelectedText, (a read/write property) you can simply replace the current SelectedText with your own text. Select 方法需要一个起始索引和select 的字符数,最后,一旦你有一个 SelectedText,(读/写属性)你可以简单地用你自己的文本替换当前的 SelectedText。 Lot easier than your current (and wrong) calculation.比您当前的(和错误的)计算容易得多。

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

相关问题 如何替换文本文件中以特定单词开头并以 C# 中的特殊字符结尾的字符串? - How to replace a string in a text file which starts with a specific word and ends with a special character in C#? C#如果行以“ *:”开头,如何选择任何字符 - C# How to select any character if line starts with “ *:” c#替换每一行中的最后一个字符 - c# replace last character in every line 您如何 state 中的正则表达式 C# 跳过一个字符,用另一个替换一个字符并在特定的 position 处添加一个新字符? - How do you state a regular expression in C# to skip a character, replace one with another and add a new character at a specific position? C# 从文件中删除以特定单词开头的行 - C# removing a line from file that starts with a specific word 如何在C#中替换一行中的特定文本 - How to replace a specific text in a line in c# 在C#中替换字符“ - Replace character " in C# c#,Winform应用程序-搜索特定行并将其替换为其他行 - c# , Winform application -search for specific line and replace it with other line 如何在C#中的RichTextBox中为特定字符换行 - How to break line for a specific character in a richtextbox in c# C#-在每个文本框行中添加内容,直到指定字符为止 - C# - Add to stuff to each textbox line till a specific character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM