Search This Blog

Tuesday, December 21, 2010

Functions

All the string functions available are methods of the System.String object.

You'll also find some are static methods of the String class. EG String.Join()

string x = "Some String;Another One";
string[] array = x.Split(";");

with InStr you have: x.Contains("Another");

replace you have x.Replace("One", "One More");

c# doesn't have direct equivalents to Left or Right, You have to use x.Substring();

nor IsNumeric. But you can do a int.TryParse

        public static int ConvertToInt(string str)
        {
            int i = 0;
            int.TryParse(str, out i);
            return i;
        }

i use a function like this to determine if a string can be parsed as an int. if the function returns 0 then it cannot be parsed.

Although you may need to create a ConvertToDecimal() function as well. Decimal has a TryParse method as well.

FYI one .NET library i didn't discover until later was the System.IO.Path library. Whenever you need to work with filesystem paths this one is your friend.

No comments:

Post a Comment