How to include an ampersand (&) in the content of a ComboBoxItem - xaml

I currently have a Combobox like the following:
//XAML
<ComboBox>
<ComboBoxItem> Awake & Alive</ComboBoxItem>
</ComboBox>
This raises an error:
Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.
I assume I am missing an escape sequence of some sort to allow me to use a &. How can I set the content of this comboboxitem to include a &?

Use & to encode the ampersand.
//XAML
<ComboBox>
<ComboBoxItem> Awake & Alive</ComboBoxItem>
</ComboBox>

The short answer is to use & to encode an ampersand.
See also Entities: Handling Special Content on XML.com:
At the lowest levels an XML parser is just a program that reads through an XML document a character at a time and analyzes it in one way or another, then behaves accordingly. It knows that it's got to process some content differently than other content. What distinguishes these special cases is the presence of such characters as "&" and "<". They act as flags to the parser; they delimit the document's actual content, alerting the parser to the fact that it must do something at this point other than simply pass the adjacent content to some downstream application.
... So one way to get around your immediate problem is to replace the ampersand in your content with the appropriate entity reference: <company>Harris & George</company>.

Alternatively, you can use the CDATA tag around the contents of the ComboBoxItem element; I think it better maintains the text's readability.
//XAML
<ComboBox>
<ComboBoxItem><![CDATA[Awake & Alive]]></ComboBoxItem>
</ComboBox>
For reference: http://www.w3schools.com/xmL/xml_cdata.asp

Related

Dojo List Text Box automatic delimiter

When we started using a Dojo List Text Box in one of our applications, I came across the problem, that this Dojo control seems to have a built in delimiter, automatically splitting every String that contains a comma into extra array items.
A code to verify this behaviour:
<xe:djextListTextBox id="djextListTextBox1"></xe:djextListTextBox><xe:valuePicker id="valuePicker1" for="djextListTextBox1">
<xe:this.dataProvider>
<xe:simpleValuePicker>
<xe:this.valueList><![CDATA[11111
222,22
33333]]></xe:this.valueList>
</xe:simpleValuePicker>
</xe:this.dataProvider>
</xe:valuePicker>
I managed to resolve the situation by manually defining another delimiter
multipleSeparator="|"
which seems to overwrite the default delimiter, but I still would be very interested in verification of this finding and experts' tips on how to handle this control properly for future reference.
Yes, it uses "," as the default delimiter.
It is defined in the dojo widget source code of _ListTextBox.js (in com.ibm.xsp.extlib.controls package, \resources\web\extlib\dijit folder. This is the base widget for several components (e.g. ListTextBox, NameTextBox, etc.) and the multiple item seperator (msep) defaults to ",".
Basically, these components keep value in a hidden inputbox and submit that value. Internally, they convert the submitted value into a vector and store into the data binding. So as long as you don't have the declared delimiter in your value list, you may use any delimiter.
One problem I had is \n, because I experienced some problems in the past. Using ";" or "," is no problem with ListTextBox. However, NameTextBox doesn't work with any delimiter other than ",". No big deal, because it's only name elements. If you use ",", this component keeps values correctly but does not render well.

Add bold formatting to localized string that has placeholders

I have a localization string with a placeholder:
Verb {0}
I use this string in my view-model to return a string to my view that, in turn, is displayed in a TextBlock. Easy enough. But a new requirement has arisen saying that the "Verb" portion (everything other than the placeholder's inserted value) be displayed in bold.
Using a string with placeholders seems like the typical and easiest way to indicate word order. So the first question, then, is: where should I parse the localization string in order to add the bold formatting? The parse operation will need knowledge of the original placeholder's location. So far, the view-model has been responsible for utilizing the localization strings by using string.Format to insert values and return its result to the view. If I leave this responsibility in the view-model, as is probably necessary, then the view-model also needs to return rich text.
Is binding to rich text even supported by RichTextBlock? Even if it is supported, I've never before had a view-model return formatted text before. It initially feels sacrilegious to a follower of MVVM-ism, but perhaps upon further consideration I may find it acceptable.
What's the best way to add bold formatting to a localized string that has placeholders? Is returning rich text from the view-model the best way?

SQL Strip the Font Format(Colour or other)

I have a problem to strip out the format in a note table
Here is an example:
";\red31\green73\blue125;
\viewkind4\uc1\ltrpar\f0\fs20 USEFUL TEXT BODY \cf1\f3
\ltrpar\f0\fs17
"
How to get rid of those stuff? I want to play safe not to replace anything after'\'
Many thanks,
Rick
Your making it quite difficult for yourself by not replace '\' .
If you look at http://other9.tripod.com/Refs/easy-rtf.html you will see that there are different RTF codes and there is no default size for the codes.
Additionally, it is not like HTML where there must be a necessary "closing" tag which makes it additionally difficult.
The only thing I can think of is to record all possible RTF codes (or use an RTF parser library) and hence be able to recognize if a \ is or is not RTF code.

Input character validation using word validation regular expression

Let's say, I have a regular expression that checks the validation of the input value as a whole. For example, it is an email input box and when user hits enter, I check it against ^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$ to see if it is a valid email address.
What I want to achieve is, I want to intercept the character input too, and check every single input character to see if that character is also a valid character. I can do this by adding an extra regular expression, e.g. [A-Z0-9._%+-] but that is not what I want.
Is there a way to extract the widest possible range of acceptable characters from a given regular expression? So in the example above, can I extract all the valid characters that are defined by the original regular expression (i.e. ^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$) programmatically?
I would appreciate any help or hint.
P.S. This is project for iOS written in Objective-C.
If you don't mind writing half a regex parser, certainly. You would have to be able to distinguish literals from meta-characters and to unroll/merge all character classes (including negated character classes, and nested negated character classes, if you regex flavor supports them).
If NSRegularExpressions doesn't come with some convenience method, I cannot imagine how it would be possible otherwise. Just think about ^. When it is outside of a character class, it's a meta-character that you can ignore. If it is inside a character class, it's a meta-character, that negates the character class unless it is not the first character. - is a meta-character inside character classes, unless it is the first character, the last character, or right after another character range (depending on regex flavor). And I'm not even speaking about escaped characters.
I don't know about NSRegularExpressions, but some flavors also support nested character classes (like [a-z[^aeiou]] for all consonants). I think you get where I am going with this.

Remove & character from string objective c

How would I go about removing the "&" symbol from a string. It's making my xml parser fail.
I have tried
[currentParsedCharacterData setString: [currentParsedCharacterData stringByReplacingOccurrencesOfString:#"&" withString:#"and"]];
But it seems to have no effect
Really what this boils down to is you want to gracefully handle invalid XML. The XML Parser is properly telling you that this XML is invalid, and is thusly failing to parse. Assuming you have no control over this XML content, I would suggest pre-parsing it for common errors like this, the output of which would be a sanitized XML doc that has a better chance of success.
To sanitize the doc, it may be as simple as doing search and replace, the problem with just doing a blanket replace on any & is that there are valid uses of &, for example & or ©. You would end up munging the XML by creating something like this: andcopy;
You could search for "ampersand space" but that won't catch a string that has an ampersand as the last character (an out-case that might be easily handled). What you are really searching for are occurrences of & that are not followed by a ; or those of which where any type of whitespace is encountered before the following ; because the semi-colon is fine on its own.
If you need more power because you need to detect this, and other errors, I would suggest going to NSScanner or RegEx matching to search for occurrences of this and other common errors during your sanitization step. It is also very common for XML files to be rather large things, so you need to be careful when dealing with these as in-memory strings as this can easily lead to application crashes. Breaking it up into manageable chunks is something NSScanner can do very well.
For a quick attempt look at stringByReplacingOccurrencesOfString on NSString
NSString* str = #"a & b";
[str stringByReplacingOccurrencesOfString:#"&" withString:#"and"]; // better replace by &
However you should also deal with other characters i.e. < >