logo       

RE: Clean way to do string.join: msg#00203

org.user-groups.dotnet.padnug

Subject: RE: Clean way to do string.join


You make a good point John, that really does look a lot cleaner.

-Matthew
Hedron Inc.
http://www.klump-pdx.com <http://www.klump-pdx.com/>
matthew@xxxxxxxxxxxxx

_____

From: John Theisen [mailto:jtheisenpdx@xxxxxxxxx]
Sent: Wednesday, January 26, 2005 11:08 AM
To: padnug@xxxxxxxxxxxxxxx
Subject: RE: [padnug] Clean way to do string.join

Stuart,
I think that the line

sb.AppendFormat ( "{0}{1}", separator, value[i] )

should be rewritten as

sb.Append(separator).Append(value[i]);

in the spirit of optimization.

-- John Theisen
jtheisenpdx@xxxxxxxxx

--- Stuart Celarier <stuart@xxxxxxxxxxx> wrote:

> Optimizing Criag's approach (and putting it in a
> method for reuse):
>
> using System.Text;
>
> public static string JoinNonempty( string separator,
> string [] value )
> {
> StringBuilder sb = new StringBuilder();
> int i = 0;
> for ( ; i < value.Length && value[i] ==
> string.Empty; ++i ) ;
>
> if ( i < value.Length )
> {
> sb.Append( value[ i++ ] );
> for ( ; i < value.Length; ++i )
> if ( value[i] != string.Empty )
> sb.AppendFormat ( "{0}{1}", separator,
> value[i] );
> }
>
> return sb.ToString();
> }
>
> Sample usage:
>
> string [] value = { "", "B", "", "D", "" };
> Console.WriteLine( JoinNonempty( " AND ", value ));
>
> This produces the output "B AND D".
>
>
> But that's a lot of logic, i.e., ample opportunity
> to get something wrong. A
> simpler approach would be to filter the string array
> first, then use the
> tested-and-tried string.Join method:
>
> Using System.Collections;
>
> public static string[] CopyNonempty( string [] value
> )
> {
> ArrayList list = new ArrayList();
> foreach ( string s in value )
> if ( s != string.Empty ) list.Add( s );
>
> return list.ToArray( typeof( string ) ) as string
> [] ;
> }
>
> Sample usage:
>
> string [] value = { "", "B", "", "D", "" };
> Console.WriteLine( string.Join( " AND ",
> CopyNonempty( value ) ) );
>
>
> All other things (like performance) being equal I
> would prefer the second
> approach. It is easier code to review and maintain,
> and there is more
> opportunity to reuse the CopyNonempty method in
> other settings.
>
> Cheers,
> Stuart
>
> Stuart Celarier | Fern Creek | www.ferncrk.com
>
>
> -----Original Message-----
> From: Craig Wagner [mailto:craig.wagner@xxxxxxxxx]
> Sent: Wednesday, January 26, 2005 8:06 AM
> To: padnug@xxxxxxxxxxxxxxx
> Subject: Re: [padnug] Clean way to do string.join
>
>
> Try this...
>
> string [] stuff = { "X", "", "Z", "" };
> StringBuilder result = new StringBuilder( 50
> );
>
> for( int i = 0; i < stuff.Length; i++ )
> {
> result.Append( ( result.Length > 0 &&
> stuff[i].Length > 0
> ? " AND " : "" ) + stuff[i] );
> }
>
> The result of the above was: X AND Z
>
> To answer your question, I think String.Join is
> behaving exactly as I
> would expect. It might be nice if it had an overload
> that allowed you
> to tell it to ignore zero-length strings, but I
> wouldn't expect the
> default behavior to do anything other than what it
> is doing.
>
> On Tue, 25 Jan 2005 18:07:54 -0800, Zhong, Clark Y
> <clark.y.zhong@xxxxxxxxx> wrote:
> >
> > Hi all:
> >
> > I like to use a lot string functions
> since formulating an
> > expression can be a pain. I tried to use
> string.join to make a simple
> > AND clause, "X AND Y AND Z", any of them can be
> empty, i.e., if X is
> > empty, it becomes "Y AND Z". However I noticed
> string.join is not that
> > smart, it does not treat empty string or null
> string differently, and
> > output " AND Y AND Z".
> >
> > I hate to do an elaborate if else
> since I need to do
> > multiple branches for different combinations. Any
> efficient way to
> > address string.join's short coming? Shouldn't it
> no do a join if string
> > element is empty?
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
>
>
>
>



_____

Yahoo! Groups Links
* To visit your group on the web, go to:
http://groups.yahoo.com/group/padnug/

* To unsubscribe from this group, send an email to:
padnug-unsubscribe@xxxxxxxxxxxxxxx
<mailto:padnug-unsubscribe@xxxxxxxxxxxxxxx?subject=Unsubscribe>

* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .


[Non-text portions of this message have been removed]






<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise