What’s the most efficient (quick) way of checking the last (ending) char in a string? I’m not sure of the best combination of string functions. NetMF seems a bit limited with string functions
if (thelastchar of thestring==“w”)
sound_the_alarm():
Also, what’s the quickest way (speed) to remove/strip off this charater from the string
char lastChar = str[str.Length - 1];
if (lastChar == 'w')
{
...
}
The reason I would not use SubStr is that is will create a new string each time it is called and result in extra GC being required esp. if called often. I admit that this is based on the desktop framework, so things might be different on .NETMF, but I imagine this would apply.
perhaps KybdBuff[2].ToString() + KybdBuff[3].ToString()
[/quote]
I think the Substring results in one allocation and the 2x ToString’s plus a concatenation results in 3 allocations…
In general, the fastest way is to avoid making new strings whenever possible. If you’re doing a number of string operations, it might be better to do them all in a char array then make a string only once you’re all done manipulating the array.
Remember that arrays are reference types, even if they are arrays of value types. So the second approach is like creating 2 “strings” the first is the array of chars which the GC will need to collect and the second is the new string that is subsequently created from the array. So now there is the extra GC and then there is the managed .NET code which is copying the data char by char from one location to the other, this is very slow.
Just using Substring is the better approach for 2 reasons.
The internal code for copying the characters from the old string to the new string will run natively optimizing for copying of blocks of data etc., so no managed code copying data from one place to another which is a huge win.
Only the single new string is created which excludes the last character.
RLP has additional overhead of marshaling the data from managed to unmanaged code, and then when it comes back you still need to create a string from it. And in all likelihood, the code will be less efficient than SubString.
Thanks for the info. In #2 I was trying to avoid an extra immutable string, but I was in essence doing that anyways apparently. Sometimes KISS is just better