Extend string with own methods
November 12, 2009
ASP.NET
C#
Nils
Snippet
Here's a nice little sample submitted by Nils Forsman on how to extend the String object.
public static class StringExtension
{
public static String AddSlashAfter(this String input)
{
return String.Concat(input, @"\");
}
public static String AddSlashBefore(this String input)
{
return String.Concat(@"\", input);
}
public static String Left(this String input, Int32 length)
{
if (length > input.Length)
{
return input;
}
return input.Substring(0, length);
}
public static String Right(this String input, Int32 length)
{
if (length > input.Length)
{
return input;
}
Int32 StartPosition = input.Length - length;
return input.S
return input.Substring(StartPosition, length);
}
public static String Mid(this String input, Int32 start, Int32 length)
{
if (start > input.Length)
{
return String.Empty;
}
if ((start + length) > input.Length)
{
return input.Substring(start);
}
return input.Substring(start, length);
}
}
You'll then able to access your new methods on any string object, for example filePath.AddSlashAfter()
Related posts: