JS_NewStringCopyN

Create a new JS string.

Syntax

JSString *
JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);

JSString *
JS_NewUCStringCopyN(JSContext *cx, const char16_t *s, size_t n);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
s const char* or const char16_t * Pointer to the array of characters containing the text for the JS string to create.
n size_t Number of characters to copy from s into the new string.

Description

JS_NewStringCopyN allocates space for a JavaScript string and its underlying storage, and copies n characters from a C character array, s, into the new JSString. JS_NewUCStringCopyN is the Unicode version of the function. The two functions differ only in the type of the character array s; both functions create ordinary JavaScript strings, and all JavaScript strings are made up of 16-bit characters.

If the array s contains more than n characters, the new string contains a truncated version of the original string. The string may contain null characters ('\0'). They are copied into the new string like any other character.

You can use JS_NewStringCopyN to copy binary data or to copy only a certain portion of a C string into a JavaScript string.

On success, JS_NewStringCopyN and JS_NewUCStringCopyN return a pointer to the new JS string. Otherwise they return NULL.

See Also