1
string phoneNumber1 = 01234567899;
string PhoneNumber2 = +441234567899;

How do I compare last 10 digits only of these 2 strings in c#? These are 2 different formats of UK phone numbers for same number. I wan't to compare them to find if they're matching.

Thanks in advance

EKD
  • 73
  • 7
  • 4
    You code doesn't compile, those are not string literals. – gunr2171 Jan 09 '23 at 14:17
  • `phoneNumber1[^10..] == PhoneNumber2[^10..]` Only works if strings are at least 10 chars. (Also, your strings are missing the quotes...) – 001 Jan 09 '23 at 14:20

3 Answers3

1

Use .TakeLast(10) to get the last 10 elements and then do the list comparison with .SequenceEqual:

    string phoneNumber1 = "01234567899";
    string phoneNumber2 = "+441234567899";

    var phoneNumber1_last10 = phoneNumber1.TakeLast(10).ToList();
    var phoneNumber2_last10 = phoneNumber2.TakeLast(10).ToList();

    // Check the result
    Console.WriteLine(phoneNumber1_last10.SequenceEqual(phoneNumber2_last10));
Manuel Fabbri
  • 542
  • 1
  • 7
  • 16
1

Approach with Reverse() and Take()

string number1 = "01234567899";
string number2 = "+441234567899";

bool result = number1.Reverse().Take(10).SequenceEqual(number2.Reverse().Take(10));
fubo
  • 44,811
  • 17
  • 103
  • 137
1

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.

Fildor
  • 14,510
  • 4
  • 35
  • 67