

Unicode standard for UnicodeString provides a unique number for every character (8, 16 or 32 bits) more than ASCII (8 bits) characters. UnicodeStrings are being used widely because of support to languages world wide and emojis. In modern C++ nowadays there are two types of strings used; array of chars (char strings) and UnicodeStrings (WideStrings and AnsiStrings are older, not compatible with all features now). CLANG / C++ Builder / GNU C / VC++ compilers, IDEs are using this standard for GUI forms to support all languages to provided applications in global. More information about the structure of Unicode Strings can be found here . RAD Studio , Delphi & C++ Builder uses Unicode-based strings: that is, the type String is a Unicode string (System.UnicodeString) instead of an ANSI string. If you want to transform your codes to Unicode strings we recommend you this article.
In general there are 4 type of alphanumeric string declarations in C++;
- Array of chars (See Fundemental Types)
chars are shaped in ASCII forms which means each character has 1 byte (8 bits) size (that means you have 0 to 255 characters) - AnsiString
Previously, String was an alias for AnsiString. For RAD Studio, C++ Builder and Delphi, the format of AnsiString has changed. CodePage and ElemSize fields have been added. This makes the format for AnsiString identical for the new UnicodeString. - WideString
WideStrings was previously used for Unicode character data. Its format is essentially the same as the Windows BSTR. WideString is still appropriate for use in COM applications. UnicodeString
(String)UnicodeString
data is in UTF-16 format that means characters in UTF-16 may be 2 or 4 bytes. There is a good article about Unicode in RadStudio. Default type ofstring
in most IDEs (i.e. RAD Studio, C++ Builder, Delphi, Visual Studio) is theUnicodeString
type. In C++ Builder & Delphi; Char and PChar types are now WideChar and PWideChar, respectively.
C++ Examples about Unicode Strings (based on CLANG / C++ Builder Compiler)
How to declare Unicode Strings
1 2 3 4 5 6 |
UnicodeString ustr = L" مرحبا"; UnicodeString ustr2 = ustr + L"Hello " + L"DEF" ; ustr=L"こんにちは"; ustr.printf( L"Pi is %8.2f", 3.14); |
L is a String Literal here, represents a wchar_t
literal; u8, u and U literals can be used too. These might be default in you editor and or compiler options that means you don’t need to add if you know the default. A string literal is a sequence of characters surrounded by double quotes, optionally prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, as in “…”, R”(…)”, u8″…”, u8R”(…)“, u”…”, uR”˜(…)˜”, U”…”, UR”zzz(…)zzz”, L”…”, or LR”(…)”, respectively. Please see String Literals section in this document Working Draft, Standard for Programming Language C++. Here below we sum some of these standards used in C++.
Examples to String Literals for Strings Definitions
- str=”abcd”; default string based on compiler/IDE options.
- str=u8″abcd”; a UTF-8 string literal and is initialized with the given characters as encoded in UTF-8, including the null terminator
- str=u”abcd”; a char16_t string literal. A char16_t string literal has type “array of n const char16_t”, including the null terminator
- str=U”abcd”; a char32_t string literal. A char32_t string literal has type “array of n const char32_t”, including the null terminator
- str=L”abcd”; a wide string literal. A wide string literal has type “array of n const wchar_t”, including the null terminator
- str=R”abcd”; raw strings
What is difference between L”” and U”” and u”” literals in C++
- L is based on wide string literal depends on array of n const wchar_t in your compiler/IDE options. Generally it is UTF-8 or UTF-16 format
- u is for UTF-16 format,
- U is for UTF32 formats
Length of Unicode String
1 2 3 4 5 |
UnicodeString ustr = L"ABCDE"; int length=ustr.Length(); if(ustr.Length()>45) ShowMessage(L"Too Long"); |
Size of Unicode String
1 2 3 4 |
UnicodeString ustr=L"ABCDEF"; int size = ustr.Length()*ustr.ElementSize(); |
How to reach / read characters of Unicode String:
1 2 3 4 |
UnicodeString ustr=L"ABCDEF"; Char Ch=ustr[3]; // Ch is 3rd C char in integer now |
How to change characters of Unicode String
1 2 3 4 5 |
ustr[3]=L'九'; //single unicode character ustr[3]=L'/u1F603'; ustr[3]=128515; |
How to find position of a string in Unicode String
1 2 3 4 5 |
UnicodeString ustr="Hey,Hello"; int pos=ustr.Pos(L"Hello"); if(ustr.Pos(L"Hello")>0) ShowMessage("Found Hello"); |
Converting Unicode String to Integer
1 2 3 4 5 |
UnicodeString ustr=L"987"; int i=ustr.ToInt(); int j=ustr.ToIntDef(0); // if not an integer then set to 0 |
Converting Unicode String to Double
Converting Unicode String to Float
1 2 3 4 |
UnicodeString ustr=L"8.45"; double i=ustr.ToDouble(); |
Converting Unicode String to LowerCase
1 2 3 4 |
UnicodeString ustr=L"This is Unicode"; UnicodeString ustr2=ustr.LowerCase(); |
Converting Unicode String to UpperCase
1 2 3 4 |
UnicodeString ustr=L"This is Unicode"; UnicodeString ustr2=ustr.UpperCase(); |
Converting Unicode String to char String
1 2 3 4 5 6 7 |
UnicodeString src=L" ABC DEF"; char *dest= ((AnsiString)src).c_str(); // we cant use c_str() of Unicode directly, we can use c_str() of AnsiString // or this can be used in some compilers System::UnicodeToUtf8( dest, 256, src, src.Length() ); |
Converting from higher number chars to lower number chars is not recommended , if you need to convert to a low level (char) that means that low level variable needs to be higher level (unicode) , otherwise you can loose some unicode characters which will result missing or wrong characters in your char strings.
Converting Unicode String to ANSI String
1 2 3 4 |
UnicodeString src=L"This is Unicode"; AnsiString dest= (AnsiString)src; |
Converting Unicode String to Wide String
1 2 3 4 5 |
UnicodeString ustr=L"This is unicode"; WideChar wstr[255]; StrCopy(wstr, ustr.w_str()); |
Substring of a Unicode String
1 2 3 4 |
UnicodeString ustr = L"ABCDEF"; UnicodeString ustr2= ustr.SubString(5, 3); |
Insert a String to UnicodeString
1 2 3 4 |
UnicodeString ustr=L"ABCDEF"; ustr.Insert(L"-insert-", 3); |
Deleting / Trimming Part of Unicode String
1 2 3 4 |
UnicodeString ustr = L"ABCDEF"; UnicodeString ustr2= ustr.Delete(2, 4); // UnicodeString& Delete(int index, int count) |
Compare Unicode Strings
1 2 3 4 5 6 |
UnicodeString ustr=L"ABCDEF"; UnicodeString ustr=L"AbCdEf"; if(ustr.Compare(ustr2)==0) ShowMessage("Sensitively Both Strings are Same "); // case-sensitive if(ustr.CompareIC(ustr2)==0) ShowMessage("Insensitively Both Strings are Same "); // case-insensitive |
Triming spaces and control characters from a Unicode String
1 2 3 4 |
ustr=L" ABC DEF"; ustr2=ustr.Trim(); |
For more details about these commands and properties listed above please check UnicodeString Mehtods & Properties and UnicodeString Types from UnicodeString wilki.