Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chrtran in .NET
Message
De
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:
01566773
Vues:
66
This message has been marked as a message which has helped to the initial question of the thread.
>>>Thanks, Greg. I'll review the code this weekend.
>>>
>>
>>Here's version two of the ChrTran with speed improvement
>>String.Length is a call to a method get_Length()
>>
>>if we loop backwards then the call is only made once instead of every iteration
>>The to.Length is also called once and stored in a variable
>>
>>
>>public static partial class ExtensionMethods
>>	{
>>
>>		public static string ChrTran(this string s, string from, string to)
>>		{
>>			var sb = new StringBuilder(s, s.Length);
>>
>>			int i, j, toLen = to.Length;
>>
>>			for (i = sb.Length; --i >= 0; )
>>			{
>>				if ((j = from.IndexOf(sb[i])) >= 0)
>>				{
>>					if (j < toLen)
>>					{
>>						sb[i] = to[j];
>>					}
>>					else
>>					{
>>						sb.Remove(i, 1);
>>					}
>>				}
>>			}
>>
>>			return sb.ToString();
>>		}
>>
>>	}
>>
>
>I was thinking we need to use multiple replace commands. Do you think this is the best implementation? I want to add this to our extension methods as I need this function also.
>
>Thanks again.


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
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform