C# TrimStart with string parameter -
i'm looking string extension methods trimstart() , trimend() accept string parameter.
i build 1 myself i'm interested in seeing how other people things.
how can done?
trimstart:
public static string trimstart(this string target, string trimchars) { return target.trimstart(trimchars.tochararray()); } trimend:
public static string trimend(this string target, string trimchars) { return target.trimend(trimchars.tochararray()); } note function trim of characters in trimchars start/end of target, e.g. "foobar;'@".trimend(";@'") return "foobar"
if instead intention trim occurrences of (exactly matching) string, should use following:
trimstart:
public static string trimstart(this string target, string trimstring) { string result = target; while (result.startswith(trimstring)) { result = result.substring(trimstring.length); } return result; } trimend:
public static string trimend(this string target, string trimstring) { string result = target; while (result.endswith(trimstring)) { result = result.substring(0, result.length - trimstring.length); } return result; }
Comments
Post a Comment