|
Re: Clean way to do string.join: msg#00200org.user-groups.dotnet.padnug
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? |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | RE: Re: VB vs C# is there a auto documenting feature in C#: 00200, Stuart Celarier |
|---|---|
| Next by Date: | RE: Clean way to do string.join: 00200, Stuart Celarier |
| Previous by Thread: | Clean way to do string.joini: 00200, Zhong, Clark Y |
| Next by Thread: | RE: Clean way to do string.join: 00200, Stuart Celarier |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |