Append comma (quote) only when required. Avoid removing them.
On Wed, Oct 14, 2009 at 6:25 AM, Benj Nunez <[email protected]> wrote: > > Hello everyone, > > I'm stumped at the moment trying to figure out how to remove a quote > (",") character from a single entry within a Stringbuilder object. I > have code that looks like this: > > > public bool parseEntries(ref BackgroundWorker worker, > ref List<StringHolder> ACVMEntries, > ref DoWorkEventArgs e) > { > ... > if (ACVMEntries != null) > { > .. > foreach (StringHolder shEntry in ACVMEntries) > { > string CVMEntry = shEntry.shType.ToLower().Trim(); > ... > > if (CVMEntry == someConst.foo1) > { > StringBuilderBin1.Append(shEntry.shOldIDNumber).Append > (","); > } > else if (CVMEntry == someConst.foo2) > { > StringBuilderBin2.Append(shEntry.shOldIDNumber).Append > (","); > } > ... // and so on... > } // of foreach > > // Remove extraneous comma at the end of each StringBuilder. > ... > StringBuilder1 = Utils.clearTrailingComma(ref StringBuilder1); > StringBuilder2 = Utils.clearTrailingComma(ref StringBuilder2); > ... > > } > > The implementation of clearTrailingComma() looks like this: > > > public static StringBuilder clearTrailingComma(ref > StringBuilder ABuilder) > { > StringBuilder TmpBuilder = ABuilder; > > if ( (TmpBuilder != null) || (TmpBuilder.Length > 0) ) > { > // Remove last quote > if (TmpBuilder[TmpBuilder.Length - 1].ToString > ().EndsWith(",")) > { > // Remove just one character at the end of the > string. > try > { > TmpBuilder.Remove(TmpBuilder.Length - 1, > 1); // Problem here*** > } > catch (System.ArgumentOutOfRangeException) > { > throw; > } > catch (System.Exception) > { > throw; > } > } > } > > return TmpBuilder; > } > > > > My issue is with a call to this line: > > > TmpBuilder.Remove(TmpBuilder.Length - 1, 1); // Problem here*** > > > This line can handle several entries within the Stringbuilder object, > but it *cannot* remove > the "," character when there is only *one* and only *one* entry. How > can this be? > > > Any advice? > > > > > > >
