BasicString.opIndex

Return slice of all character.

The slice returned may be invalidated by further calls to other member functions that modify the object.

  1. inout(CharType)[] opIndex()
    struct BasicString
    inout return pure nothrow @system @nogc
    static if(allowHeap)
    inout(CharType)[]
    opIndex
    ()
  2. inout(CharType)[] opIndex()
  3. CharType opIndex(size_t pos)

Examples

()@system{
    BasicString!char str = "123";

    char[] slice = str[];
    assert(slice.length == str.length);
    assert(slice.ptr is str.ptr);

    str.reserve(str.capacity * 2);
    assert(slice.length == str.length);
    assert(slice.ptr !is str.ptr);  // slice contains dangling pointer.
}();
()@safe{
    FixedString!(char, 20) str = "123";

    char[] slice = str[];
    assert(slice.length == str.length);
    assert(slice.ptr is str.ptr);

    assert(slice == "123");
}();

Meta