Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

StringBuilder.AppendJoin (appending lists to StringBuilder) #8303

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/mscorlib/src/System/Text/StringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace System.Text {
using System.Threading;
using System.Globalization;
using System.Diagnostics.Contracts;
using Collections.Generic;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this using not follow the same convention as the previous ones, i.e. to include System even when it's not necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I do not fully understand. It should be changed to

using Collections;
using Collections.Generic;

Or what?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant changing it to:

using System.Collections.Generic;

All the old usings do that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed


// This class represents a mutable string. It is convenient for situations in
// which it is desirable to modify a string, perhaps by removing, replacing, or
Expand Down Expand Up @@ -1011,6 +1012,108 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
return this;
}

// Append joined values with a separator between each value.
public StringBuilder AppendJoin<T>(string separator, IEnumerable<T> values)
{
Contract.Ensures(Contract.Result<StringBuilder>() != null);

Contract.Requires<ArgumentNullException>(values != null, nameof(values));

using (var en = values.GetEnumerator())
{
if (!en.MoveNext())
return this;

var value = en.Current;
if (value != null)
Append(value.ToString());

while (en.MoveNext())
{
Append(separator);
value = en.Current;
if (value != null)
Append(value.ToString());
}
}
return this;
}

// Append joined values with a separator between each value.
public StringBuilder AppendJoin<T>(string separator, params T[] values)
{
Contract.Ensures(Contract.Result<StringBuilder>() != null);

Contract.Requires<ArgumentNullException>(values != null, nameof(values));

if (values.Length == 0)
return this;

var value = values[0];
if (value != null)
Append(value.ToString());

for (var i = 1; i < values.Length; i++)
{
Append(separator);
value = values[i];
if (value != null)
Append(value.ToString());
}
return this;
}

// Append joined values with a separator between each value.
public StringBuilder AppendJoin<T>(char separator, IEnumerable<T> values)
{
Contract.Ensures(Contract.Result<StringBuilder>() != null);

Contract.Requires<ArgumentNullException>(values != null, nameof(values));

using (var en = values.GetEnumerator())
{
if (!en.MoveNext())
return this;

var value = en.Current;
if (null != value)
Append(value.ToString());

while (en.MoveNext())
{
Append(separator);
value = en.Current;
if (null != value)
Append(value.ToString());
}
}
return this;
}

// Append joined values with a separator between each value.
public StringBuilder AppendJoin<T>(char separator, params T[] values)
{
Contract.Ensures(Contract.Result<StringBuilder>() != null);

Contract.Requires<ArgumentNullException>(values != null, nameof(values));

if (values.Length == 0)
return this;

var value = values[0];
if (null != value)
Append(value.ToString());

for (var i = 1; i < values.Length; i++)
{
Append(separator);
value = values[i];
if (null != value)
Append(value.ToString());
}
return this;
}

/*====================================Insert====================================
**
==============================================================================*/
Expand Down