在所选行的开头添加一个字符(TAB) [英] Add a character(TAB) to begin of selected lines
本文介绍了在所选行的开头添加一个字符(TAB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在所选文本的每一行开头添加TAB(或其他任何字符)?
我只找到了这些代码,但是其余的我不知道如何
How can I add a TAB(or any other character) to the begin of each line in the selected text?
I only found these Pieces of code, but I don''t know how to do the rest
RichTextBox1.SelectionStart;
RichTextBox1.SelectionLength;
推荐答案
可以使用吗?
\ t
以及我们是否可以根据上面的张贴者要求提供有关该问题的更多信息?
Can you use this?
\t
And can we have some more info on the problem as the poster above requested?
1.编写一个循环遍历文本的循环.
2.每次检测到换行符时,都必须在该索引中添加"\ t".
1. Write a loop that will go through the text.
2. Every time you detect a new line character you will have to add a "\t" in that index.
尝试以下操作:
private void button4_Click(object sender, EventArgs e)
{
// get the selected text
string selText = allText.Substring(richTextBox1.SelectionStart, richTextBox1.SelectionLength);
// get the collection of Lines
string[] rtfLines = richTextBox1.Lines;
string theLine;
for (int i = 0; i < rtfLines.Length; i++)
{
// compare each line
theLine = rtfLines[i];
// to see if it's in the selection
if (selText.Contains(theLine))
{
// in selection : add a tab
rtfLines[i] = "\t" + theLine;
}
}
// reset the content of the RichTextBox
// this necessary because changing a line in the
// collection of Lines does not affect the RTf content
richTextBox1.Lines = rtfLines;
}
}
请注意,这只会在当前所选内容完全包含的行的开头添加一个制表符.如果一行中有部分选择,将被忽略.
可以实现部分选择的选项卡,但这是另一章:也许您会写一个?
Note this only will add a tab at the start of lines that are fully contained in the current selection. If you have a partial selection in a line, that will be ignored.
Tabbing in a partial selection could be achieved, but that''s another chapter: perhaps one you will write ?
这篇关于在所选行的开头添加一个字符(TAB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文