This might do the trick for you if the RTF
is a formatted string.
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
would contain data something like this {\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
... and when we try to do the operation on this kind of string it will throw error which would be File format is not valid but to get saved from this problem we can use SelectedText
or Text
of RichTextBox where we only get the Inner Text of the RichTextBox. and then we can replace text with Upper Text.
Edit
After the requirements of changing Unicode Strings to Uppercase. The RTF change the tôi tên là
to something like this \cf1\f1\fs20 t\'f4i t\'ean l\'e0
and when we run the code Replace(strText, strText.ToUpper());
for line tôi tên là
cannot be found in the RichTextBox1.Rtf
so I used a if condition to see if the string exsist in the rtf or not if yes then normal code would work but if the unicode string exsist then changed it to rtf to see what is the rtf version of the unicode string.
Another one is Stephan Bauer
rightly suggested that if r
or rtf
or t
,f
,rt
,tf
exsist in the line than it would give error which changing the case. so Added that functionality as well.
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());
}
}
}
This is how we get all the Substrings in the String of 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);
}
}