[Haskell-cafe] Reversing a string of words: C# v Perl V Ruby v Haskell

Neil Mitchell ndmitchell at gmail.com
Sun Dec 10 09:50:25 EST 2006


Hi

> > A popular C# approach goes something like:
> >
> >  private static string reverseWords(string str) {
> >      string[] words = Array.FindAll<string>(str.Split(
> >                       new char[] {' ','\t'}),
> >                       delegate(string s) {
> >                          return !String.IsNullOrEmpty(s); });
> >      int i = words.Length - 1;
> >      if (i < 0)
> >          return String.Empty;
> >      StringBuilder sb = new StringBuilder(words[i]);
> >      while (--i >= 0)
> >          sb.Append(' ').Append(words[i]);
> >      return sb.ToString();
> >  }

Why not: String.Join(" ", words) ?

Saves all the hassle with StringBuilder etc.

Thanks

Neil


More information about the Haskell-Cafe mailing list