|
RE: Clean way to do string.join: msg#00202org.user-groups.dotnet.padnug
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 > > > > > > > > > > > > |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | RE: Clean way to do string.join: 00202, Stuart Celarier |
|---|---|
| Next by Date: | RE: Clean way to do string.join: 00202, Matthew Klump |
| Previous by Thread: | RE: Clean way to do string.joini: 00202, Stuart Celarier |
| Next by Thread: | RE: Clean way to do string.join: 00202, Matthew Klump |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |