csharp Left, Right and Mid examples

public static void leftRightMid_Demo()
	string myString = "This is a string";

	//4 characters from the left

	//6 characters from the right

	//4 characters starting at index 5 of the string

	//characters from index 5 up to the end of the string

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:24pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.