Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chrtran in .NET
Message
 
 
À
22/02/2013 13:07:49
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01360482
Message ID:
01566775
Vues:
38
>Can be optimized in that case
>
>would be like this
>
>public static string ChrTran(this string s, string[]  from, string[] to)
>
>
>Of course - the length of the arrays from and to must be equal
>
>It's an extra loop. sb needs to be instantiated only once, and only once sb.ToString();
>
>
>Also, in the oiginal code above from and to are not checked for null values

Here is the code I have in VFP
 lcInvalidChars   = chr(0) + chr(1) + chr(2) + chr(3) + chr(4) + chr(5) + chr(6) + chr(7) + chr(11) + ;
                               chr(12) + chr(14) + chr(15) + chr(16) + chr(17) + chr(18) + chr(19) + chr(20) + chr(21) + chr(22) + ;
                               chr(23) + chr(24) + chr(25) + chr(26) + chr(27) + chr(28) + chr(29) + chr(30) + chr(31)
            tvFieldValue   = chrtran(tvFieldValue, lcInvalidChars, ' ')
And this is what I just added to our functions using your code in this thread:
/// <summary>
        /// ChrTran
        /// <B>Re: Chrtran in .NET</B> Thread #1360482 Message #1360903
        /// </summary>
        /// <param name="s"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static string ChrTran(this string s, string from, string to)
        {
            // check parameters
            if (String.IsNullOrEmpty(s))
                return s;

            if (String.IsNullOrEmpty(from))
                return s;


            // (1) Split from into translate and remove

            // "1234", "56"
            // translate = "12"
            // remove = "34"
            char[] translate, remove;

            // "1234", ""
            if (String.IsNullOrEmpty(to))
            {
                translate = new char[0];

            }
            // "1234", "5678"
            // "1234", "56"
            // "1234", "A"
            // "1234", "567890"  - can happen
            else
            {
                translate = new char[Math.Min(from.Length, to.Length)];
                from.CopyTo(0, translate, 0, translate.Length);
            }

            remove = new char[from.Length - translate.Length];
            from.CopyTo(translate.Length, remove, 0, remove.Length);

            // (2) remove chars from s - but not those that will be translated
            //	avoid	StrTran("AA", "B")
            //			translate = "A", to = "B"
            //			remove = "A"
            var sb = new StringBuilder(s, s.Length);
            int i, j;

            for (i = remove.Length; --i >= 0; )
            {
                if (!translate.Contains(remove[i]))
                    sb.Replace(remove[i].ToString(), null);
            }

            // (3) translate

            // if no char in from is in to, then a fast translate can be done
            //  if not, we'll have to loop

            if (translate.Length > 0)
            {
                bool fastTranslate = true;

                for (i = translate.Length; --i >= 0; )
                {
                    if (to.Contains(from[i]))
                    {
                        fastTranslate = false;
                        break;
                    }
                }

                if (fastTranslate)
                {
                    for (i = translate.Length; --i >= 0; )
                    {
                        sb.Replace(translate[i], to[i]);
                    }
                }
                else
                {
                    for (i = sb.Length; --i >= 0; )
                    {
                        if ((j = Array.IndexOf<char>(translate, sb[i])) >= 0)
                            sb[i] = to[j];
                    }
                }
            }
            return sb.ToString();
        }
So, assuming this is correct, how would I call it for the above case?

Thanks again.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform