Can we make C# look more like XPath?

Why, the answer is quite positive and just to prove this yesterday Oleg Tkachenko published his string extension methods substringBefore() and substringAfter().

Great!

But he forgot one of my favourite — translate().

Here is a two-minutes-lunchbreak implementation: 

public static string 
          translate(this string source, 
                             string repPat, string repImg)
 
        { 
            StringBuilder sb 
                 = new StringBuilder(source);  

            int indPat = 0; 
            int lrepImg = repImg.Length; 

            foreach (char c in repPat.ToCharArray()) 
            { 
                if (indPat < lrepImg) 
                    sb.Replace(c, repImg[indPat]); 
                else 
                    sb.Replace(c.ToString(), "");  

                indPat++; 
            } 
            return sb.ToString(); 
        }

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Can we make C# look more like XPath?

  1. M. David says:

    Dimitre,This *ROCKS*! :D  More please! :D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s