views:

134

answers:

2

I have a function within a custom CRM web application (old VB.Net circa 2003) that takes a set of fields from a database and merges them with palceholders in a set of RTF based template documents. These generate merged letters and documentation. The code essentially loops through each line of the RTF template file and replaces any instances of the placeholder values with text from a database record. The issue I'm having is that users have pasted a certain type of apostrophe into the web app (and therefore into the database) that is not rendering correctly in the resulting RTF file. It is rendering like this - ’.

I need a way to spot this invalid apostrophe in the code and replace it with a valid one. Unfortunately when I paste the invalid apostrophe into the Visual Studio editor it gets converted into the correct one. So I need another way to express this invalid apostrophe's value. Unfortunately I do not know a great deal about unicode and other encodings so I'm calling out for help with this.

Any ideas?

A: 

It looks like you probably want to encode characters out of the 8 bit range (>255).

You can do that using \uNNNN according to the wikipedia article.

Colin Newell
+1  A: 

If you really just want to figure out what the character is you might want to try and paste it into a text editor like ultraedit. It has a hex mode that you can flip to to see the actual underlying bytes.

In order to do the replace once you've figured out the character you'd do something like this in Vb,

text.Replace(ChrW(2001), "'")

Note that you might not be able to figure it out easily using the text editor because it might also get mangled by paste from the clipboard. You might want to either print some debug of the ascii values from code. You can use the AscW function to do that.

I can't help but think that it may actually simply be a case of specifying the correct encoding to use when you write out the stream though. Assuming you're using a StreamWriter you can specify it on the constructor. I'm guessing you actually want ASCII given your requirement.

    oWriter = New System.IO.StreamWriter(path, False, System.Text.Encoding.ASCII)
Colin Newell
The code looks something like this: somestring = somestring.Replace("’", "'")I'm not sure if the different apostrophe will show up properly but essentially I need to replace the first (dodgy) apostrophe with the second one. But when I type this line of code in VS it uses the same apostrophe for both, so it never works. Are you able to provide an example of using Hex characters in a line of code like this? Thanks.
j.strugnell
I've updated the answer with more information.
Colin Newell
j.strugnell