You can also make it reusable. Below are some extension methods created after the XLST functions substring-beforeand substring-afterto make it general.
Using: var userNm = (User.Identity.Name.Contains(@"\") ? User.Identity.Name.SubstringAfter(@"\") : User.Identity.Name);
public static class StringExt {
public static string SubstringAfter(this string s, string searchString) {
if (String.IsNullOrEmpty(searchString)) return s;
var idx = s.IndexOf(searchString);
return (idx < 0 ? "" : s.Substring(idx + searchString.Length));
}
public static string SubstringBefore(this string s, string searchString) {
if (String.IsNullOrEmpty(searchString)) return s;
var idx = s.IndexOf(searchString);
return (idx < 0 ? "" : s.Substring(0, idx));
}
}
source
share