-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
110 lines (101 loc) · 3.95 KB
/
Copy pathStringExtensions.cs
File metadata and controls
110 lines (101 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Net;
using System.Text.RegularExpressions;
using Geta.Net.Extensions;
using Microsoft.AspNetCore.Http;
namespace Geta.Optimizely.Extensions
{
/// <summary>
/// String extensions.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Strip all HTML elements from a HTML string and return text.
/// </summary>
/// <param name="htmlText">A HTML string.</param>
/// <param name="maxLength">Max string length to return. 0 returns all text in the HTML string.</param>
/// <returns>A string with text.</returns>
public static string StripHtml(this string htmlText, int maxLength = 0)
{
if (string.IsNullOrWhiteSpace(htmlText))
return htmlText;
var stripped = Regex.Replace(htmlText, "<[^>]*>", string.Empty, RegexOptions.None, TimeSpan.FromSeconds(1));
stripped = WebUtility.HtmlDecode(stripped);
return maxLength > 0 && stripped.Length > maxLength
? stripped[..maxLength]
: stripped;
}
/// <summary>
/// Parses string to nullable int (Int32).
/// </summary>
/// <param name="input">Source string.</param>
/// <returns>int (Int32) value if parse succeeds otherwise null.</returns>
public static int? TryParseInt32(this string input)
{
int outValue;
return Int32.TryParse(input, out outValue) ? (int?)outValue : null;
}
/// <summary>
/// Parses string to nullable long (Int64).
/// </summary>
/// <param name="input">Source string.</param>
/// <returns>long (Int64) value if parse succeeds otherwise null.</returns>
public static long? TryParseInt64(this string input)
{
long outValue;
return long.TryParse(input, out outValue) ? (long?)outValue : null;
}
/// <summary>
/// Adds scheme and host to a relative URL.
/// </summary>
/// <param name="input">URL</param>
/// <param name="context">HttpContext.</param>
/// <returns>Returns URL with scheme and host.</returns>
public static string GetExternalUrl(this string input, HttpContext context)
{
return AddHost(input, context);
}
/// <summary>
/// Removes scheme and host from the URL.
/// </summary>
/// <param name="url">URL</param>
/// <returns>Returns URL without scheme and host.</returns>
public static string RemoveHost(this string url)
{
if (url.IsAbsoluteUrl())
{
var uri = new Uri(url);
return uri.PathAndQuery;
}
return url ?? string.Empty;
}
/// <summary>
/// Adds scheme and host to a relative URL. Uses UriHelpers.GetBaseUri() to retrieve base URL for the scheme and host.
/// </summary>
/// <param name="url">URL</param>
/// <param name="context">HttpContext</param>
/// <returns>Returns URL with scheme and host.</returns>
public static string AddHost(this string url, HttpContext context)
{
return url.AddHost(context.GetBaseUri);
}
/// <summary>
/// Adds scheme and host to a relative URL.
/// </summary>
/// <param name="url">URL</param>
/// <param name="getBaseUri">Function which returns base URL.</param>
/// <returns>Returns URL with scheme and host.</returns>
public static string AddHost(this string url, Func<Uri> getBaseUri)
{
var baseUri = getBaseUri();
var uri = new Uri(baseUri, url).ToString();
var uriBuilder = new UriBuilder(uri) { Scheme = baseUri.Scheme };
if (baseUri.IsDefaultPort)
{
uriBuilder.Port = -1;
}
return uriBuilder.ToString();
}
}
}