Question Replace Certain Strings Completely in A RichTextBox

misteralex

Member
Joined
Mar 16, 2016
Messages
8
Programming Experience
Beginner
Hey guys,
so I got one RichTextBox that has a combination of normal text and some unicode. It looks like this:


"Blablabla /u0063 /u0063 blablab
blablabla /u0055 /u0023"


I figured out through MSDN how to convert the /u0063 into actual letters and symbols etc. I need to take all the strings that begin with a
VB.NET:
/u
and put it into my Converter Code/Function!


Now the question is: HOW do I EFFICIENTLY and effectively go through the COMPLETE RichTextbox and replace ALL the unicodes with their respective sign/actual letter?


Should I use RegEx? Should I use arrays (someone suggested that, not sure though)? Or are there certain Text/String Manipulation Methods to specifically achieve this?
 
I would use Regex since this is pattern matching, use Regex.Replace with a MatchEvaluator function for replacing matches.
Get the number part of the strings and use Integer.Parse to convert the hex string first, then Convert.ToChar that number.
This code combines these three operations:
Dim result = Regex.Replace(input, "/u([0-9a-fA-F]{4})", Function(m) Convert.ToChar(Integer.Parse(m.Groups(1).Value, NumberStyles.HexNumber)))
 
The replace namespace doesnt work because of the Function(m) Convert [...] part. Something needs to be corrected there but I can't figure out what...
 
Alright, I am getting one step further towards my goal.

My question now is, how can I update the regex search string

VB.NET:
Regex.Replace(RichTextBox2.Text, [B]"\/u\d+"[/B], New MatchEvaluator(AddressOf ConvertUnicode))

to something that also looks for letters like /u064a for instance?

Thanks so much guys
 
Last edited:
See post 2, the regex there handles all hex strings.
 
Thanks John, I updated it.

The codes that are in the listboxes actually start with \u , so I updated the search pattern accordingly.

I want to replace the hex codes for each entry in the listbox2 , so I tried it this way

VB.NET:
For xx = 0 To txtZahl.Text - 1                          
                            Regex.Replace(ListBox2.Items(xx), "\\u([0-9a-fA-F]{4})", New MatchEvaluator(AddressOf ConvertUnicode))
                            
                        Next

txtZahl is a textbox that includes the number of listbox rows. So if I enter like "3" in txtZahl.Text, then the listbox rows will be 3 as well.

But when I do the regex.replace, it doesn't work for some reason. Does it only work with textboxes or something? Or (and this is what I'm assuming) something with the search pattern/string is not correct. The ConvertUnicode function works in another app.

One example of how things look

Row 1: Blah so good \ud83d\ude09
Row 2: Thank you @asdasd and @dd for my IV blah\/blah today. #yada
Row 3: Hello hello \ud83d\ude1b\ud83d\udcaf
 
I figured it out, you need to insert the row before the regex.replace as well!

I figured it out!


I needed to put the row there as well


VB.NET:
For xx = 0 To txtZahl.Text - 1                          
                            [B]Listbox2.Items[/B](xx) = Regex.Replace(ListBox2.Items(xx), "\\u([0-9a-fA-F]{4})", New MatchEvaluator(AddressOf ConvertUnicode))
                            
                        Next
 
I don't get one thing though!

When I run my program within vb.NET, it does everything correctly and it works.

But when I run the stand alone exe file, it runs into an error and can't convert a certain hexcode. How is that possible? WHy does it work wihtin visual studios without any issues and the standalone exe runs into an error converting it?!?!
 
Back
Top