0

I have an xml file, When writing to the file, i want autorized only the following character:

a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G  H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
/ - ? : ( ) . , ‘ +, SPACE

So if I have an unauthorized character, I replace this character by an authorized , for example:

  • [é, è, ê] replaced by e

  • [î, ï] replaced by i

  • [ü, û] replaced by u

  • ç replaced by c

  • [#,! @] Replaced by SPACE

    So how I should proceed, if I declare four containers

  • Container CE= ['é', 'è', 'ê'] ;

  • Container CI= ['î', 'ï'] ;

  • Container CRIEN = ['°' , '#', '!', '@'] ;

  • container CU= ['û','ü'];

Text example :

anytype con = ['çdéjeunè 123 & south st @ Chicago, ILî 60652'];
    
Community
  • 1
  • 1
Ahmed Oubaha
  • 1
  • 1
  • 1

3 Answers3

0

One of the ways to do it:

static void ReplaceChars(Args _args)
{
    container fromCon   = ['é', 'è', 'ê', 'î', 'ï', 'ü', 'û', 'ç', '#', '!', '@'];
    container toCon     = ['e', 'e', 'e', 'i', 'i', 'u', 'u', 'c', ' ', ' ', ' '];
    str s = 'çdéjeunè 123 & south st @ Chicago, ILî 60652'; 
    int i, pos;

    info(s);

    for (i = 1; i <= strLen(s); i++)
    {
        pos = conFind(fromCon, subStr(s, i, 1));
        if (pos > 0)
        {
            s = subStr(s, 1, i - 1) + conPeek(toCon, pos) + subStr(s, i + 1, strLen(s) - i);
        }
    }

    info(s);
}
10p
  • 5,488
  • 22
  • 30
0

Also take a look at this post: How do I remove diacritics from a string in dynamics ax

It uses the static method

System.Globalization.CharUnicodeInfo::GetUnicodeCategory(Char _ch)

to remove the diacritics. It appears a bit more complex at first, but covers all characters you might encounter later in your XML files, rather than having to list them all (You will surely forget about one or two, causing exceptions later)

Since you want some exceptions, as with #, !, @ for example, you need to do those 'manually'. A good robust solution would be a combo of the neat method described above by 10p to first do all the special chars you want to turn into spaces, and then use the method above (See code in related post for complete job) to get rid of all the standard äÄïÏ etc.

Community
  • 1
  • 1
Wessel du Plooy
  • 505
  • 5
  • 18
0

Use the answer I provided in your first question How do I remove diacritics from a string in dynamics ax to first get a string of the friendly-format.

Then take that string and use strKeep(...) function to keep only the characters you want. http://msdn.microsoft.com/en-us/library/aa867746.aspx

Here is the updated job. You should mark the answers as correct though unless they don't answer it for you. Here is the updated job that does your exact string:

static void JobRemoveDiacritics(Args _args)
{
    str strInput = 'çdéjeunè 123 & south st @ Chicago, ILî 60652';
    System.String input = strInput;
    str retVal;
    int i;

    System.Char c;
    System.Text.NormalizationForm FormD = System.Text.NormalizationForm::FormD;
    str normalizedString = input.Normalize(FormD);
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

    for (i = 0; i <= strLen(normalizedString); i++)
    {
        c = System.Char::Parse(subStr(normalizedString, i, 1));

        if (System.Globalization.CharUnicodeInfo::GetUnicodeCategory(c) != System.Globalization.UnicodeCategory::NonSpacingMark)
        {
            stringBuilder.Append(c);
        }
    }

    input = stringBuilder.ToString();
    input = input.Normalize();
    retVal = input;
    retVal = strKeep(retVal, 'abcdefghijklmnopqrstuvwxyz0123456789/-?:().,\'+ ');

    info(strFmt("Before: '%1'", strInput));
    info(strFmt("After: '%1'", retVal));
}
Community
  • 1
  • 1
Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
  • Thank you @Alex K , i have one exception, that of character & I want to transform it into + this solution displays : Before: 'çdéjeunè 123 & south st @ Chicago, ILî 60652 &&' After: 'cdejeune 123 south st Chicago, ILi 60652 ' I want it to appear : After: 'cdejeune 123 + south st Chicago, ILi 60652 ++' – Ahmed Oubaha Sep 10 '14 at 15:46
  • You need to use the `strReplace` function then. You need to do some coding yourself here. – Alex Kwitny Sep 10 '14 at 17:12