csharp Left, Right and Mid examples
public static void leftRightMid_Demo()
{
string myString = "This is a string";
//4 characters from the left
Console.WriteLine(Left(myString,7));
//6 characters from the right
Console.WriteLine(Right(myString,6));
//4 characters starting at index 5 of the string
Console.WriteLine(Mid(myString,5,4));
//characters from index 5 up to the end of the string
Console.WriteLine(Mid(myString,5));
}
public static string Left(string param, int length)
{
if (length > param.Length)
length = param.Length;
return param.Substring(0, length);
}
public static string Right(string param, int length)
{
return param.Substring((param.Length - length), length);
}
public static string Mid(string param, int startIndex, int length)
{
return param.Substring(startIndex, length);
}
public static string Mid(string param, int startIndex)
{
return param.Substring(startIndex);
}
//Extension method alternative for Left()
public static string Left(this string str, int length)
{
if (length > str.Length) length = str.Length;
return str.Substring(0, length);
}VB's Left(), Right() and Mid() implemented in C#
Updated: Tuesday 3rd May 2011, 06:03pm
There are 0 comments
Comments are currently closed.