插入标记词 [英] insert marked word

查看:102
本文介绍了插入标记词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

我正在制作一个html编辑器,并且我想拥有一个在标记的单词之前和之后输入HTML标记的函数.这是代码:

Hi.

I''m making a html-editor and I want to have a function that inputs HTML-tags before and after a marked word. This is the code:

string strInsertBefore = "<a>";
string strInsertAfter = "</a>";

string text = textBox4.Text.Substring(textBox4.SelectionStart, textBox4.SelectionLength);

text =  strInsertBefore + text + strInsertAfter;

string strInsert = text;

textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart, strInsert);



这会导致< a> hello</a> hello.

但我希望它导致< a"你好</a>仅.
但是我无法正常工作!

这是我尝试过的代码,用于删除标记的单词.



This results in <a>hello</a>hello .

but i want it to result in <a>hello</a> only.
But i can''t get it to work!

Here is a code that I''ve tried which removes the marked word.

textBox4.Text.Remove(textBox4.SelectionStart, textBox4.SelectionLength);



谢谢!



Thank you!

推荐答案

您是否考虑过将两个功能结合在一起?
首先,从文本框中删除单词.
然后,插入由HTML标记包围的单词...
Have you considered combining the two functions?
First, remove the word from your textbox.
Then, insert the word, surrounded by the HTML tags...


为什么不直接在正确的位置插入strInsertBefore strInsertAfter

why don''t you simply insert strInsertBefore and strInsertAfter at the right places

textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart, strInsertBefore );

textBox4.Text = textBox4.Text.Insert(textBox4.SelectionStart + textBox4.SelectionLength + strInsertBefore.Length, strInsertAfter);



事情是未经测试的代码,则需要根据需要调整第二个代码的索引.



thing is untested code, you will need to adjust the index for the second one as needed.


这篇关于插入标记词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆