Add Char to String?
Hi I have a string that goes like this
string s1 = "Hey";
I need to add a byte value of 3 (for string length) to beginning of the string. So it Will begin with 0x03. How can I do this?
What is the easiest way to do this?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Jun 2, 2007, 5:04 PM
This is the easiest way:
byte b = 0x03;
string s1 = "Hey";
s1 = (char)b + s1;
Notice that in .NET strings always consist of unicode characters and you therefore need to convert the byte to a char before prepending it to the string.