Obtaining last string character

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”)

Also, what’s the quickest way (speed) to remove/strip off this charater from the string

LastChar = str.Substr(str.length);
str = str.substr(0, str.length - 1);

Forgive casing and lack of code tags replying from phone.

Char c = str[str.Length - 1];

I would suggest the following

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.

Good call on the GC issue

[quote]Also, what’s the quickest way (speed) to remove/strip off the last character from the string

any speedy ways?

Also, is there a better way to represent:

KybdBuff.Substring(2, 2)

perhaps KybdBuff[2].ToString() + KybdBuff[3].ToString()

any speedy ways?

Also, is there a better way to represent:

KybdBuff.Substring(2, 2)

perhaps KybdBuff[2].ToString() + KybdBuff[3].ToString()
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.

any thoughts on this?.. (my prior example was not to remove a character)

My guesses are:

(the obvious)
#1 string newStr = oldStr.substring(0, oldStr.length - 1)


#2 you could create a char[] to hold the new value, copy all but the last into the array using oldStr[index] and then stuff it back into a string

string oldStr = "this is the older string value";
            char[] newVal = new char[oldStr.Length - 1];

            for (int i = 0; i < oldStr.Length - 1; i++)
                newVal[i] = oldStr[i];

            string newStr = new string(newVal);


Just thought of option #3 – would RLP work better for this?

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.

  1. 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.
  2. 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 :slight_smile:

@ mhectorgato - It is easy to fall into these traps, in fact when I saw the question I went through all the same mental gymnastics.