358
Console.Write("πΆπ₯Γ©-a\u0304\u0308bc\u0327".UnicodeReplaceAtFastest(5, "π") + " using UnicodeReplaceAtFastest(5, 'π') - cool but still O(100) more than string.Replace :(");
1
using System.Linq;
2
using System.Diagnostics;
3
using System;
4
using System.Text;
5
using System.Globalization;
6
β
7
//Created for
8
//http://metadataconsulting.blogspot.com/2019/03/A-Unicode-ReplaceAt-string-extension-method-handles-Unicode-string-properly.html
9
β
10
public static class Program
11
{
12
const char cEMPTY = '\0';
13
static readonly string EMPTY = cEMPTY.ToString();
14
15
/// <summary>
16
/// A proper Unicode ReplaceAt fast method, that handles surrogate pairs and 4-byte characters after the Basic Multilingual Plane (BMP) > U+FFFF
17
/// </summary>
18
/// <param name="s">input Unicode string</param>
19
/// <param name="idx">index of nth character in string to be replaced at (not zero based, not based on length)</param>
20
/// <param name="replace">Unicode replacement</param>
21
public static string UnicodeReplaceAtFastest(this string s, int idx, string replace)
22
{
23
// This StringBuilder holds the output results.
24
StringBuilder sb = new StringBuilder();
Cached Result