如果RTF 是格式化字符串,这可能会为您解决问题。
RichTextBox1.SelectAll();
string[] textArray = RichTextBox1.SelectedText.Split(new char[] { '\n', '\t' });
foreach(string strText in textArray)
{
if(!string.IsNullOrEmpty(strText) && strText != "rtf")
RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(strText, strText.ToUpper());
}
RichTextBox.Rtf 将包含类似 {\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang17417{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\trowd\trgaph108\trleft5\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs... 的数据,当我们尝试对这种字符串进行操作时,它会抛出错误,即 文件格式无效但是为了避免这个问题,我们可以使用 RichTextBox 的 SelectedText 或 Text,我们只获得 RichTextBox 的内部文本。然后我们可以用大写文本替换文本。
编辑
在将 Unicode 字符串更改为大写的要求之后。 RTF 将tôi tên là 更改为类似\cf1\f1\fs20 t\'f4i t\'ean l\'e0 的内容,当我们运行tôi tên là 行的代码Replace(strText, strText.ToUpper()); 时,在RichTextBox1.Rtf 中找不到行,所以我使用了if 条件来查看字符串是否存在于rtf 与否,如果是,则正常代码将起作用,但如果 unicode 字符串存在,则将其更改为 rtf 以查看 unicode 字符串的 rtf 版本。
另一个是 Stephan Bauer 正确地建议如果 r 或 rtf 或 t,f,rt,tf 存在于该行中,则它会给出错误,从而改变大小写。所以也添加了该功能。
string somevar = "rtf"; //string to eliminate
List<string> subStrings = new List<string>();
for(int i = 0; i < somevar.Length; i++)
{
subStrings.AddRange(GetAllSubStrings(somevar, i + 1));
}
RichTextBox1.Rtf = RichTextBox1.Rtf.ToLower();
string[] textArray = RichTextBox1.Text.Split(new char[] { '\n', '\t' });
foreach(string strText in textArray)
{
if(!string.IsNullOrEmpty(strText))
if(!subStrings.Any(x => strText == x))
{
if(RichTextBox1.Rtf.Contains(strText))
{
RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(strText, strText.ToUpperInvariant());
}
else
{
RichTextBox rt = new RichTextBox();
rt.Text = strText;
string rtftext = rt.Rtf.Substring(rt.Rtf.IndexOf("fs17") + 4);
rtftext = rtftext.Substring(0, rtftext.IndexOf("par")-1);
RichTextBox1.Rtf = RichTextBox1.Rtf.Replace(rtftext, strText.ToUpperInvariant());
}
}
}
这就是我们获取rtf字符串中所有子字符串的方式
public static IEnumerable<string> GetAllSubStrings(string input, int length)
{
for(var i = 0; i < input.Length - length + 1; i++)
{
yield return input.Substring(i, length);
}
}