Sorting & Replacing Accented Characters
Summary | This article describes how to sort lists with accents as well as replacing accented characters in a string with their non-accented equivalent. |
to | AskiaDesign |
Written for | Survey Programmers |
Keywords | Caption, Sort, Find, Replace, Text, Array, Change response order, Accents, Special characters. |
Download the example 5.4.9.0. compatible QEX file here.
In the QEX file you will see two different examples:
- C2 - Ordering a list so that all accented characters are listed amongst their non-accented equivalent e.g. Aaron, Ádept, Ænima, Alfred, Álpha.
- C3 - Replacing all accented characters in a string and replacing them their non-accented equivalents e.g. Éàster becomes Easter and ẞloss becomes Ssloss.
Please note, example C2 (the routing setting into QX2) requires Design version 5.4.9.0 or higher where we can sort text arrays.
Example C2
The syntax uses a predefined list of accented characters, a separator and then its non-accented letter equivalent. See variable QACCENTS:
We create two text arrays using the text from either side of the pipe '|' separator in this case. arrFind & arrReplace.
Our code-frame to sort is in QX2. We scroll through its response captions in the variable tempstring (QX2.Responses[j].Caption):
We replace any 'accented' characters with their non-accented counterparts using the find and replace text arrays mentioned previously.
We can now create two further text arrays, and finding the index of arrCaptionsCleanSort in arrCaptionsCleanNOSort will give us the sort order which arranges things alphabetically.
Dim arrFind = {""} Dim arrReplace = {""} Dim i
For i = 1 To QACCENTS.Responses.Count arrFind = arrFind.Insert(QACCENTS.Responses[i].Caption.Split("|")[1]) arrReplace = arrReplace.Insert(QACCENTS.Responses[i].Caption.Split("|")[2]) Next i arrFind = arrFind.RemoveAt(1) arrReplace = arrReplace.RemoveAt(1) Dim arrCaptionsClean = {""} Dim tempString = "" Dim tempString2 = "" Dim tempArray = {} Dim j Dim k For j = 1 To QX2.Responses.Count tempString = QX2.Responses[j].Caption For k = 1 to arrFind.Count tempString = tempString.Replace(arrFind[k].Tostring(), arrReplace[k].Tostring(), true) Next k arrCaptionsClean.Insert(tempString) Next j arrCaptionsClean.RemoveAt(1) Dim arrCaptionsCleanNOSort = arrCaptionsClean Dim arrCaptionsCleanSort = arrCaptionsClean.Sort() Dim arrSortOrder = {} Dim h
For h = 1 To arrCaptionsClean.Count tempString2 = arrCaptionsCleanSort[h].ToString() arrSortOrder.Insert(arrCaptionsCleanNOSort.IndexOf(tempstring2)) Next h Return arrSortOrder
Example C3
This starts similarly to the syntax above with the find and replace text arrays. If we detect any accent/special character entered in the variable, accentedtext, we replace it with its non-accented counterpart and return this 'cleaned' text.
Dim j
For j = 1 To arrFind.Count
If (accentedtext.value).IndexOf(arrFind[j].ToString() , true) > 0 Then
strReplace = strReplace.Replace(arrFind[j].ToString(), arrReplace[j].ToString())
Endif
Next j
Return strReplace