Skip to content

Deprecated StringExtensions.AsFormat extension #3798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Microsoft.Toolkit/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
Expand Down Expand Up @@ -38,17 +39,17 @@ public static class StringExtensions
/// <summary>
/// Regular expression for removing comments from HTML.
/// </summary>
private static readonly Regex RemoveHtmlCommentsRegex = new Regex("<!--.*?-->", RegexOptions.Singleline);
private static readonly Regex RemoveHtmlCommentsRegex = new("<!--.*?-->", RegexOptions.Singleline);

/// <summary>
/// Regular expression for removing scripts from HTML.
/// </summary>
private static readonly Regex RemoveHtmlScriptsRegex = new Regex(@"(?s)<script.*?(/>|</script>)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
private static readonly Regex RemoveHtmlScriptsRegex = new(@"(?s)<script.*?(/>|</script>)", RegexOptions.Singleline | RegexOptions.IgnoreCase);

/// <summary>
/// Regular expression for removing styles from HTML.
/// </summary>
private static readonly Regex RemoveHtmlStylesRegex = new Regex(@"(?s)<style.*?(/>|</style>)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
private static readonly Regex RemoveHtmlStylesRegex = new(@"(?s)<style.*?(/>|</style>)", RegexOptions.Singleline | RegexOptions.IgnoreCase);

/// <summary>
/// Determines whether a string is a valid email address.
Expand Down Expand Up @@ -145,7 +146,17 @@ public static string FixHtml(this string html)
/// <param name="format">The format of the string being linked.</param>
/// <param name="args">The object which will receive the linked String.</param>
/// <returns>Truncated string.</returns>
public static string AsFormat(this string format, params object[] args) => string.Format(format, args);
[Obsolete("This method will be removed in a future version of the Toolkit. Use the native C# string interpolation syntax instead, see: https://docs.microsoft.com/dotnet/csharp/language-reference/tokens/interpolated")]
public static string AsFormat(this string format, params object[] args)
{
// Note: this extension was originally added to help developers using {x:Bind} in XAML, but
// due to a known limitation in the UWP/WinUI XAML compiler, using either this method or the
// standard string.Format method from the BCL directly doesn't always work. Since this method
// doesn't actually provide any benefit over the built-in one, it has been marked as obsolete.
// For more details, see the WinUI issue on the XAML compiler limitation here:
// https://github.com/microsoft/microsoft-ui-xaml/issues/2654.
return string.Format(format, args);
}

/// <summary>
/// Truncates a string to the specified length.
Expand Down