StringExtensions
Defines static String extension methods.
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
namespace Relativity.DataExchange
{
internal static class StringExtensions
{
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "The original implementation was explicitly designed to throw NullReferenceException.")]
public static string ToDelimitedFileCellContents(this string input, string bound, string newlineProxy)
{
return input.Replace("\r\n", newlineProxy).Replace("\r", newlineProxy).Replace("\n", newlineProxy)
.Replace(bound, bound + bound);
}
public static string ToCsvCellContents(this string input)
{
return input.ToDelimitedFileCellContents("\"", "\n");
}
public static string ToSqlFriendlyName(this string input)
{
return Regex.Replace(input ?? string.Empty, "[\\W]+", string.Empty);
}
public static string AppendTrailingSlashToUrl(this string url)
{
if (string.IsNullOrWhiteSpace(url))
return string.Empty;
if (url.LastIndexOf('/') != url.Length - 1)
url += "/";
return url;
}
public static string TrimLeadingSlashFromUrl(this string url)
{
if (string.IsNullOrWhiteSpace(url))
return string.Empty;
return url.TrimStart('/', '\\');
}
public static string TrimTrailingSlashFromUrl(this string url)
{
if (string.IsNullOrWhiteSpace(url))
return string.Empty;
return url.TrimEnd('/', '\\');
}
public static string CombineUrls(this string url, string value)
{
if (string.IsNullOrWhiteSpace(url))
return string.Empty;
url = url.TrimTrailingSlashFromUrl().AppendTrailingSlashToUrl() + value.TrimLeadingSlashFromUrl();
return url;
}
}
}