|
-
Jul 16th, 2010, 09:23 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Replace characters in RichTextBox?
I'm using something like this:
vb.net Code:
Dim tmp As String = RichTextBox.Text
tmp = tmp.Replace("[", "{")
tmp = tmp.Replace(Space(1), "_")
How do I find all the "Enters" and replace them with a "^"?
I have tried vbcrlf and chr(13), but nothing
-
Jul 16th, 2010, 09:30 AM
#2
Re: Replace characters in RichTextBox?
It's important to understand what a line break is. In Windows, the standard line break is a combination of a carriage return character (ASCII 13) and a line feed character (ASCII 10). This combination can be represent in various ways. Those who've worked with C-based languages will recognise "\r\n", which is not supported in VB. In VB you can use vbCrLf, ControlChars.CrLf, ControlChars.NewLine or Environment.NewLine. On non-Windows systems, the standard line break is a line feed character alone, which can be represented using vbLf or ControlChars.Lf.
If I remember correctly, the RichTextBox uses line feeds only for line breaks. Maybe that's a standard for RTF, I don't know, but it would explain why your searching for CR/LF or just CR didn't work: you need to search for just LF.
For completeness, what you might like to do is first trying replacing all CR/LF pairs, then replace lone LFs. That way you will be sure to get every line break regardless of whether it's the Windows or non-Windows type.
-
Jul 16th, 2010, 09:32 AM
#3
Re: Replace characters in RichTextBox?
Also, why do this:
vb.net Code:
tmp = tmp.Replace(Space(1), "_")
when you could just do this:
vb.net Code:
tmp = tmp.Replace(" ", "_")
Also, I think that you'll find that, if your replacements involve only a single character in and out, it's probably more efficient to actually use Chars instead of Strings:
vb.net Code:
tmp = tmp.Replace(" "c, "_"c)
-
Jul 16th, 2010, 09:38 AM
#4
Thread Starter
Addicted Member
Re: Replace characters in RichTextBox?
Thanks for the detailed explanation. I appreciate it.
-
Jul 16th, 2010, 10:57 AM
#5
Re: Replace characters in RichTextBox?
Just a little addendum...
On non-Windows systems, the standard line break is a line feed character alone, which can be represented using vbLf or ControlChars.Lf.
And Environment.NewLine ... that's the point of Environment.NewLine ... it is driven by the environment. Which is why it's the recommended way to do linefeeds.
http://msdn.microsoft.com/en-us/libr...t.newline.aspx
 Originally Posted by MSDN
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
Also, I don't think RTF actually stores the LF character... seems I remember seeing it storing the RTF code for it {\lf} I think... or something like that... I also know there's a "para" identifier to denote paragraphs... not sure how that fits into all of this... never mind, I see you're dealing with the .Text property... in which case jmc is right... replace the LFs.
-tg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|