StringBuilder.GetChunks StringBuilder.GetChunks StringBuilder.GetChunks StringBuilder.GetChunks Method

定义

返回一个对象,该对象可用于循环访问从此 StringBuilder 实例创建的 ReadOnlyMemory<Char> 中表示的字符区块。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

返回

ReadOnlyMemory<Char> 中区块的枚举器。An enumerator for the chunks in the ReadOnlyMemory<Char>.

注解

可以用如下代码来循环访问内存范围内的块: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 */ }

此方法的目的是有效地提取常量 StringBuilder的数据。The purpose of this method is to efficiently extract the data of a constant StringBuilder. 如果在StringBuilder块区枚举不完整时修改了, 则结果是不确定的。If the StringBuilder is modified while the chunk enumeration is incomplete, the result is undefined. StringBuilder也不是线程安全的, 因此在并发线程上对其进行操作是非法的。StringBuilder is also not thread-safe, so operating on it with concurrent threads is illegal.

如果修改了, 则返回的ReadOnlyMemory<T>块不会保持不变, 因此, 不会将它们缓存供以后使用。 StringBuilderThe ReadOnlyMemory<T> chunks returned are not guaranteed to remain unchanged if the StringBuilder is modified, so do not cache them for later use.

从创建ReadOnlyMemory<T>.Span (正如在上一示例中执行的操作) 非常昂贵, 因此, 如果需要在嵌套for语句中使用局部变量, 请为范围创建一个本地变量。 ReadOnlyMemory<T> ReadOnlySpan<T>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] */ 
    }
}

适用于