C# 如何在文本中加粗字符串
我的问题和这个类似 我想在标签之间添加粗体字符串 我用你的线C# 如何在文本中加粗字符串,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我的问题和这个类似 我想在标签之间添加粗体字符串 我用你的线 string pattern = @"<strong>(.*?)</strong>"; var matches = Regex.Matches(paragraf2.Range.Text, pattern) .Cast<Match>() .Select(m => m.Groups[1].Value)
string pattern = @"<strong>(.*?)</strong>";
var matches = Regex.Matches(paragraf2.Range.Text, pattern)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
但是我不知道里面写什么。与您的案例相关(如果描述正确),您可以通过修改字符串使用[]中给出的解决方案:
richTextBox1.SelectedText = richTextBox1.SelectedText.Replace("<b>", "").Replace("</b>", "");
Rgds,如har07在评论中所述,您可以像
string pattern = @"<strong>(.*?)</strong>";
var matches = Regex.Matches(paragraf2.Range.Text, pattern)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
Word.Find findObject = Application.Selection.Find;
foreach( string a in matches){
findObject.ClearFormatting();
findObject.Text = a;
findObject.Replacement.ClearFormatting();
findObject.Replacement.Font.Bold = 1;
object replaceAll = Word.WdReplace.wdReplaceAll;
findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
字符串模式=@“(.*)”;
var matches=Regex.matches(pararaf2.Range.Text,模式)
.Cast()
.Select(m=>m.Groups[1]。值)
.ToList();
Word.Find findObject=Application.Selection.Find;
foreach(匹配中的字符串a){
findObject.ClearFormatting();
findObject.Text=a;
findObject.Replacement.ClearFormatting();
findObject.Replacement.Font.Bold=1;
object replaceAll=Word.WdReplace.wdReplaceAll;
findObject.Execute(ref缺失、ref缺失、ref缺失、ref缺失、ref缺失、ref缺失、ref缺失、ref缺失、ref替换、ref缺失、ref缺失、ref缺失、ref缺失、ref缺失、ref缺失);
}
您尝试了什么?您的标记似乎表明您正在将生成的文本放入Word文档中。是吗?@KevinS是的。我想要的是在word文档中找到强化文本(匹配的文本提供了字符串列表)并替换为粗体文本。我不想全部删除,我需要粗体文本。遗憾的是,它不起作用。没有范围。SelectionFont或range.text.SelectionFont
richTextBox1.SelectedText = richTextBox1.SelectedText.Replace("<strong>", String.Empty).Replace("</strong>", String.Empty);
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string regexString = "(?<=<strong>)(.*?)(?=</strong>)";
Match matches = (Regex.Match(richTextBox1.Text, regexString));
if (matches.Success)
{
int index1 = richTextBox1.Find("<strong>");
int index2 = richTextBox1.Find("</strong>");
richTextBox1.Select(index1 + 3, ((index2) - (index1 + 3)));
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Bold);
}
}
private void ToggleBold()
{
if (richTextBox1.SelectionFont != null)
{
System.Drawing.Font currentFont = richTextBox1.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (richTextBox1.SelectionFont.Bold == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}
richTextBox1.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
string pattern = @"<strong>(.*?)</strong>";
var matches = Regex.Matches(paragraf2.Range.Text, pattern)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
Word.Find findObject = Application.Selection.Find;
foreach( string a in matches){
findObject.ClearFormatting();
findObject.Text = a;
findObject.Replacement.ClearFormatting();
findObject.Replacement.Font.Bold = 1;
object replaceAll = Word.WdReplace.wdReplaceAll;
findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}