StringBuilder.GetChunks Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an object that can be used to iterate through the chunks of characters represented in a ReadOnlyMemory<Char>
created from this StringBuilder instance.
public:
System::Text::StringBuilder::ChunkEnumerator GetChunks();
public System.Text.StringBuilder.ChunkEnumerator GetChunks ();
member this.GetChunks : unit -> System.Text.StringBuilder.ChunkEnumerator
Public Function GetChunks () As StringBuilder.ChunkEnumerator
Returns
An enumerator for the chunks in the ReadOnlyMemory<Char>
.
Remarks
You can iterate the chunks in the memory range with code like the following:
foreach (ReadOnlyMemory<char> chunk in sb.GetChunks())
foreach(char ch in chunk.Span)
{ /* operations on ch */ }
The purpose of this method is to efficiently extract the data of a constant StringBuilder. If the StringBuilder is modified while the chunk enumeration is incomplete, the result is undefined. StringBuilder is also not thread-safe, so operating on it with concurrent threads is illegal.
The ReadOnlyMemory<T> chunks returned are not guaranteed to remain unchanged if the StringBuilder is modified, so do not cache them for later use.
Creating a ReadOnlySpan<T> from a ReadOnlyMemory<T> (as the ReadOnlyMemory<T>.Span method does in the previous example) is expensive, so create a local variable for the span if you need to use it in a nested for
statement. For example:
foreach (ReadOnlyMemory<char> chunk in sb.GetChunks())
{
var span = chunk.Span;
for(int i = 0; i < span.Length; i++)
{
/* operations on span[i] */
}
}
Applies to
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기