nsACString str; str.BeginWriting(5); // reserve space for 5 characters, including null terminator strcpy(str.BeginWriting(), "hello"); // copy "hello" to the buffer str.SetLength(5); // truncate to the actual length
nsACString str("abcd"); char* buf = str.BeginWriting(); // get pointer to buffer for (int i = 0, j = str.Length() - 1; i < j; ++i, --j) { std::swap(buf[i], buf[j]); // swap characters }In both examples, we use the BeginWriting method to obtain a writable buffer for the string. The first example sets the contents of the string to a constant string literal, while the second example reverses the characters in the string. Both examples use the automatic memory management and multibyte encoding support provided by the nsACString class. Overall, nsACString is part of the Mozilla Gecko SDK library, and can be used for efficient string manipulation with automatic memory management and multibyte encoding support.