18c339a94Sopenharmony_ci# String 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ciClass `Napi::String` inherits from class [`Napi::Name`][]. 48c339a94Sopenharmony_ci 58c339a94Sopenharmony_ci## Constructor 68c339a94Sopenharmony_ci 78c339a94Sopenharmony_ci```cpp 88c339a94Sopenharmony_ciNapi::String::String(); 98c339a94Sopenharmony_ci``` 108c339a94Sopenharmony_ci 118c339a94Sopenharmony_ciReturns a new **empty** `Napi::String` instance. 128c339a94Sopenharmony_ci 138c339a94Sopenharmony_ciIf an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not 148c339a94Sopenharmony_cibeing used, callers should check the result of `Env::IsExceptionPending` before 158c339a94Sopenharmony_ciattempting to use the returned value. 168c339a94Sopenharmony_ci 178c339a94Sopenharmony_ci```cpp 188c339a94Sopenharmony_ciNapi::String::String(napi_env env, napi_value value); ///< Wraps a Node-API value primitive. 198c339a94Sopenharmony_ci``` 208c339a94Sopenharmony_ci- `[in] env` - The environment in which to create the string. 218c339a94Sopenharmony_ci- `[in] value` - The primitive to wrap. 228c339a94Sopenharmony_ci 238c339a94Sopenharmony_ciReturns a `Napi::String` wrapping a `napi_value`. 248c339a94Sopenharmony_ci 258c339a94Sopenharmony_ciIf an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not 268c339a94Sopenharmony_cibeing used, callers should check the result of `Env::IsExceptionPending` before 278c339a94Sopenharmony_ciattempting to use the returned value. 288c339a94Sopenharmony_ci 298c339a94Sopenharmony_ci## Operators 308c339a94Sopenharmony_ci 318c339a94Sopenharmony_ci### operator std::string 328c339a94Sopenharmony_ci 338c339a94Sopenharmony_ci```cpp 348c339a94Sopenharmony_ciNapi::String::operator std::string() const; 358c339a94Sopenharmony_ci``` 368c339a94Sopenharmony_ci 378c339a94Sopenharmony_ciReturns a UTF-8 encoded C++ string. 388c339a94Sopenharmony_ci 398c339a94Sopenharmony_ci### operator std::u16string 408c339a94Sopenharmony_ci```cpp 418c339a94Sopenharmony_ciNapi::String::operator std::u16string() const; 428c339a94Sopenharmony_ci``` 438c339a94Sopenharmony_ci 448c339a94Sopenharmony_ciReturns a UTF-16 encoded C++ string. 458c339a94Sopenharmony_ci 468c339a94Sopenharmony_ci## Methods 478c339a94Sopenharmony_ci 488c339a94Sopenharmony_ci### New 498c339a94Sopenharmony_ci```cpp 508c339a94Sopenharmony_ciNapi::String::New(); 518c339a94Sopenharmony_ci``` 528c339a94Sopenharmony_ci 538c339a94Sopenharmony_ciReturns a new empty `Napi::String`. 548c339a94Sopenharmony_ci 558c339a94Sopenharmony_ci### New 568c339a94Sopenharmony_ci```cpp 578c339a94Sopenharmony_ciNapi::String::New(napi_env env, const std::string& value); 588c339a94Sopenharmony_ciNapi::String::New(napi_env env, const std::u16::string& value); 598c339a94Sopenharmony_ciNapi::String::New(napi_env env, const char* value); 608c339a94Sopenharmony_ciNapi::String::New(napi_env env, const char16_t* value); 618c339a94Sopenharmony_ciNapi::String::New(napi_env env, const char* value, size_t length); 628c339a94Sopenharmony_ciNapi::String::New(napi_env env, const char16_t* value, size_t length); 638c339a94Sopenharmony_ci``` 648c339a94Sopenharmony_ci 658c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Value` object. 668c339a94Sopenharmony_ci- `[in] value`: The C++ primitive from which to instantiate the `Napi::Value`. `value` may be any of: 678c339a94Sopenharmony_ci - `std::string&` - represents a UTF8 string. 688c339a94Sopenharmony_ci - `std::u16string&` - represents a UTF16-LE string. 698c339a94Sopenharmony_ci - `const char*` - represents a UTF8 string. 708c339a94Sopenharmony_ci - `const char16_t*` - represents a UTF16-LE string. 718c339a94Sopenharmony_ci- `[in] length`: The length of the string (not necessarily null-terminated) in code units. 728c339a94Sopenharmony_ci 738c339a94Sopenharmony_ciReturns a new `Napi::String` that represents the passed in C++ string. 748c339a94Sopenharmony_ci 758c339a94Sopenharmony_ciIf an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not 768c339a94Sopenharmony_cibeing used, callers should check the result of `Env::IsExceptionPending` before 778c339a94Sopenharmony_ciattempting to use the returned value. 788c339a94Sopenharmony_ci 798c339a94Sopenharmony_ci### Utf8Value 808c339a94Sopenharmony_ci```cpp 818c339a94Sopenharmony_cistd::string Napi::String::Utf8Value() const; 828c339a94Sopenharmony_ci``` 838c339a94Sopenharmony_ci 848c339a94Sopenharmony_ciReturns a UTF-8 encoded C++ string. 858c339a94Sopenharmony_ci 868c339a94Sopenharmony_ci### Utf16Value 878c339a94Sopenharmony_ci```cpp 888c339a94Sopenharmony_cistd::u16string Napi::String::Utf16Value() const; 898c339a94Sopenharmony_ci``` 908c339a94Sopenharmony_ci 918c339a94Sopenharmony_ciReturns a UTF-16 encoded C++ string. 928c339a94Sopenharmony_ci 938c339a94Sopenharmony_ci[`Napi::Name`]: ./name.md 94