#include "nsString.h" void modifyString(nsAString& str) { char16_t* buffer = str.BeginWriting(); buffer[0] = u'A'; buffer[1] = u'B'; buffer[2] = u'C'; }
#include "nsString.h" void appendString(nsAString& str1, nsAString& str2) { char16_t* buffer1 = str1.BeginWriting(); const char16_t* buffer2 = str2.BeginReading(); int32_t length1 = str1.Length(); int32_t length2 = str2.Length(); for (int i = 0; i < length2; i++) { buffer1[length1 + i] = buffer2[i]; } }In this example, we define a function that takes two nsAString objects by reference and appends the contents of the second string to the end of the first string. We use the BeginWriting method to obtain a pointer to the buffer of the first string, and the BeginReading method to get a read-only pointer to the buffer of the second string. We then use a loop to copy the characters from the second string buffer into the appropriate location in the first string buffer.