ReadOnlySpan<char>
- Version:
public static bool IsMatch(ReadOnlySpan<char> a, ReadOnlySpan<char> b)
{
var a10 = a[^10..];
var b10 = b[^10..];
return a10.Equals(b10, StringComparison.Ordinal);
}
Usable as var isMatch = IsMatch(phoneNumber1 , phoneNumber2 );
=> https://dotnetfiddle.net/43wNR9
I would also recommend to maybe take into consideration creating a "PhoneNumber" type, that parses Country-Code if present and the rest of number? And then you can create EqualityComparer, override Equals
...
If you need to call this very often, you should consider this:
BenchmarkDotNet=v0.13.3, OS=Windows 10 (10.0.19044.2364/21H2/November2021Update)
Intel Core i9-10885H CPU 2.40GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.101
[Host] : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|------------------ |-----------:|----------:|----------:|-------:|----------:|
| MemoryCompareSpan | 5.564 ns | 0.1313 ns | 0.1228 ns | - | - |
| ReverseTake | 467.598 ns | 8.5469 ns | 7.9948 ns | 0.0629 | 528 B |
| TakeLast | 629.914 ns | 4.7967 ns | 4.4868 ns | 0.1068 | 896 B |
// * Hints *
Outliers
Benchmark.MemoryCompareSpan: Default -> 1 outlier was removed, 3 outliers were detected (6.72 ns, 6.76 ns, 7.31 ns)
Benchmark.TakeLast: Default -> 1 outlier was detected (620.55 ns)
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
For when they match and
BenchmarkDotNet=v0.13.3, OS=Windows 10 (10.0.19044.2364/21H2/November2021Update)
Intel Core i9-10885H CPU 2.40GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.101
[Host] : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|------------------ |-----------:|----------:|----------:|-------:|----------:|
| MemoryCompareSpan | 4.708 ns | 0.1113 ns | 0.1041 ns | - | - |
| ReverseTake | 277.765 ns | 2.3049 ns | 2.1560 ns | 0.0629 | 528 B |
| TakeLast | 583.637 ns | 7.0153 ns | 6.5621 ns | 0.1068 | 896 B |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
when they don't.