Remove last character from Stringbuilder single entry

2,262 views
Skip to first unread message

Benj Nunez

unread,
Oct 13, 2009, 8:55:06 PM10/13/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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?






Raghupathi Kamuni

unread,
Oct 14, 2009, 12:57:04 AM10/14/09
to dotnetde...@googlegroups.com
Append comma (quote) only when required. Avoid removing them.

Vitaly Maslevskiy

unread,
Oct 14, 2009, 3:20:14 AM10/14/09
to dotnetde...@googlegroups.com
I would agree with Raghupathi Kamuni add commas when you need it

If it is not solution for you then add comma before entry like

 foreach (StringHolder shEntry in ACVMEntries)
     {

<skiped>
          StringBuilder.Append(",").Append(shEntry.shOldIDNumber);
} // of foreach

// Remove extraneous comma at the end of each StringBuilder.
...

if (StringBuilder.Length>0)
StringBuilder.Remove(0,1); //remove first char if stringbuilder
has some entries

2009/10/14 Raghupathi Kamuni <raghu...@gmail.com>

Andrew Badera

unread,
Oct 14, 2009, 8:35:45 AM10/14/09
to dotnetde...@googlegroups.com
Or, if .Length == 1, why not just .Replace(",", "") ... ?

But that said, I'm guessing there's a fairly simple algorithmic flaw
underlying this problem ...

∞ Andy Badera
+1 518-641-1280
∞ This email is: [ ] bloggable [x] ask first [ ] private
∞ Google me: http://www.google.com/search?q=andrew%20badera

Arsalan Tamiz

unread,
Oct 15, 2009, 12:37:38 AM10/15/09
to dotnetde...@googlegroups.com
I can't find a real bug in your code but here are my suggestions

1) StringBuilderBin1.Append(shEntry.shOldIDNumber).Append(",");

in my opinion (maybe NOT correct) above line should be written like this

StringBuilderBin1.Append(shEntry.shOldIDNumber + ",");

2) "clearTrailingComma" <- this is actually a function returning "StringBuilder" so there is NO need to pass the same thing by reference. Simply don't use "ref" here.
3) "if ( (TmpBuilder != null) || (TmpBuilder.Length > 0) )" <- you should use "&&" instead of "||"
4) "if (TmpBuilder[TmpBuilder.Length - 1].ToString ().EndsWith(","))" <- this check is strange, you are getting the last character, converting it to string, and then using the "EndsWith" why? obviously it would be a single character.
5) In your function "clearTrailingComma" you should use parameter directly, there is NO need to again assign that parameter to some other variable.
6) Put some break points and check the values at different points of your code

Regards,
Arsalan Tamiz

himanshu kaushik

unread,
Oct 15, 2009, 8:44:35 AM10/15/09
to dotnetde...@googlegroups.com
u can use remove and substring function for remove char from string

Benj Nunez

unread,
Oct 15, 2009, 8:56:20 PM10/15/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks Arsalan for your tips. I'll consider this on my other
projects. :)

Here's what I did which solved my problem:


public static StringBuilder clearTrailingComma(ref
StringBuilder ABuilder)
{
string Holder = ABuilder.ToString();

// Bad code: Holder.Remove(Holder.Length - 1, 1);
Holder = Holder.TrimEnd(',');

StringBuilder TmpBuilder = new StringBuilder(Holder);

return TmpBuilder;
}



Regards,


Benj





On Oct 15, 12:37 pm, Arsalan Tamiz <sallus...@gmail.com> wrote:
> I can't find a real bug in your code but here are my suggestions
>
> 1) StringBuilderBin1.Append(shEntry.shOldIDNumber).Append(",");
>
> in my opinion (maybe NOT correct) above line should be written like this
>
> StringBuilderBin1.Append(shEntry.shOldIDNumber + ",");
>
> 2) "clearTrailingComma" <- this is actually a function returning
> "StringBuilder" so there is NO need to pass the same thing by reference.
> Simply don't use "ref" here.3) "if ( (TmpBuilder != null) ||
> (TmpBuilder.Length > 0) )" <- you should use "&&" instead of "||"
> 4) "if (TmpBuilder[TmpBuilder.Length - 1].ToString ().EndsWith(","))" <-
> this check is strange, you are getting the last character, converting it to
> string, and then using the "EndsWith" why? obviously it would be a single
> character.
> 5) In your function "clearTrailingComma" you should use parameter directly,
> there is NO need to again assign that parameter to some other variable.
> 6) Put some break points and check the values at different points of your
> code
>
> Regards,
> Arsalan Tamiz
>

ARUN KUMAR

unread,
Oct 18, 2009, 12:50:55 AM10/18/09
to dotnetde...@googlegroups.com

Hello Benj Nunez,

                           Here I mention what you miss in your code

TmpBuilder.Remove(TmpBuilder.Length - 1, 1); // problem  code

TmpBuilder.Remove(TmpBuilder.ToString().Length - 1, 1);// solution to problem

@ Arsalan

                Mr.Arsalan  I thing your suggestion(First point only) is wrong because  Use of StringBuilder is remove string operation (concatenation )   within String buffer.

For eg.

 

StringBuilderBin1.Append(shEntry.shOldIDNumber + ",");

When you use  above code

Two Immutable string concat to new instance of string then finally append to string buffer

 

in my opinion  below  line is correct

StringBuilderBin1.Append(shEntry.shOldIDNumber).Append(",");

Here doesn’t create any instance of string it only append to string buffer

2009/10/16 Benj Nunez <benj...@gmail.com>



--
Regards
*******************
*C.Arun Kumar *
*******************



Arsalan Tamiz

unread,
Oct 19, 2009, 12:21:03 AM10/19/09
to dotnetde...@googlegroups.com
Ok thanks for the info.
Reply all
Reply to author
Forward
0 new messages