Thursday, March 3, 2011

Substrings & C#

To things simple, I decided that every value I'm writing in my "ContentField" will end with ", ". But this also happens with the last element. It doesn't look that good, that's why I want to cut it off.
And here's my solution:
int strlength = ContentField.Length;
string strend = ContenField.Substring(strlength - 2);
if (strend == ", ")
{
    strlength = strlength -2;
}
ContentField = ContentField.Substring(0, strlength);
I need the length of the string, in this case: the length of  "ContentField". To keep things shorter, I want to write that value into a new variable:
int strlength = ContentField.Length;
 The only interesting part in that string are the last too letters, so I'm creating a substring, only viewing the last 2 characters:
string strend = ContenField.Substring(strlength - 2);
The rest ist simple: compare the string to the characters, cut them off and post the new string.
ContentField = ContentField.Substring(0, strlength);
In this case I decided that the start of my substring is 0, this is the first character. and the end of my string will be defined by "strlength".

1 comment:

  1. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial

    Thanks a lot..!

    ReplyDelete