Use this myString = Convert::ToChar(MyVariable0); myString += Convert::ToChar(MyVariable1); myString += Convert::ToChar(MyVariable2); If you get a problem with starting a string do this myString = Convert::ToString(Convert::ToChar(MyVariable0)); myString += Convert::ToChar(MyVariable1); myString += Convert::ToChar(MyVariaqble2);
Category: Strings
Arrays of Strings
Creating String Arrays Different ways to create string arrays: array<String^> ^myArray = gcnew array<string^>(10); //or array<String^> ^myArray; myArray = gcnew array<String^>(10); //or array<String^> ^myArray = {"channel8" "channel9", "technet" , "channel10", "msdn"}; Calling a function with an array of constant strings MyFunction((gcnew array {"Monday", "Tuesday", "Wednesday"}); Getting each entry from a list of strings array ^ports; […]
Convert Bytes To A String
array<Byte> ^RxData = gcnew array<Byte>(2); RxData[0] = 'A'; RxData[0] = 'B'; String ^sTemp; sTemp = System::Text::Encoding::UTF8->GetString(RxData); N.B. Make the array the length of the string – do not use a 0x00 terminating byte. If you do you get a resulting string but you can't add any new characters to it (e.g. MyString += "abc") because […]
Convert String To Byte Array
String Direct To Byte Array String ^sTemp; array<Byte> ^TxData; sTemp = “Hello”; TxData = System::Text::Encoding::UTF8->GetBytes(sTemp); Lengthy Method String ^Filename; Filename = “HELLO.TXT”; array<Char> ^NameArray = Filename->ToCharArray(); Array::Resize(NameArray, 13); //If you need to ensure a minimum size use this TxData[ByteId++] = Convert::ToByte(NameArray[0]); TxData[ByteId++] = Convert::ToByte(NameArray[1]); TxData[ByteId++] = Convert::ToByte(NameArray[2]); TxData[ByteId++] = Convert::ToByte(NameArray[3]); TxData[ByteId++] = Convert::ToByte(NameArray[4]); TxData[ByteId++] = […]
Convert String To Unicode Values
String Conversion To Unicode Byte Array using namespace System::Text; array<Byte> ^MyArray = Encoding::Unicode->GetBytes(MyString); using namespace System::Text; Encoding ^Unicode1 = Encoding::Unicode; array<Byte> ^UnicodeBytes = Unicode1->GetBytes("1234"); //Produces: 0x31,0x00,0x32,0x00,0x33,0x00,0x34,0x00 Convert Unicode Bytes To String using namespace System::Text; MyString = Encoding::Unicode->GetString(MyByteArray); Then To Convert To Unicode Words int Count; UInt16 UnicodeValue; for (Count = 0; Count < UnicodeBytes->Length; Count […]
Display Values In A String
Display Values With Decimal Places When formatting numbers you can use “0” as mandatory place and “#” as optional place. Display Decimal Value Display Decimal Places Display Hex Value Display Binary Value Display Software Version Typical Solution Display DateTime Link to all format codes Good Examples http://blogs.msdn.com/b/kathykam/archive/2006/03/29/564426.aspx Another Example
Is String Value Numeric
int i = 0; bool ValueIsNumeric = int::TryParse(MyStringValue, i); For larger values: UInt64 i = 0; ValueIsNumeric = UInt64::TryParse(MyStringValue, i);
Sanitising strings for use in SQL statements
If possible don't sanitize your strings. Use parameterized queries instead, as they handle all sanitization. For MS-SQL //Convert single quotes to two single quotes TagDescription = TagDescription->Replace("'", "''"); For MySQL //Convert single quotes to two single quotes TagDescription = TagDescription->Replace("'", "\'");
String Builder
When you want to manipulate and edit strings use StringBuilder rather than String. StringBuilder^ myString = gcnew StringBuilder(“Hello World”, 30); //Start off with 30 character capacity (will auto grow as needed)
Strings General
The string type is a library type and is actaully an array of char or wchar_t. The last character in a string is always null. The library takes care of managing the memory associated with storing the elements. As well as being less powerful, C-style strings are the root cause of many many security problems, […]