Skip to content

Commit 250348a

Browse files
authored
Merge pull request #87 from shinji-san/feature-RemoveSeparators
Add `RemoveSeparators` method to process strings without separators Resolves: #87
2 parents c423f11 + aa71b66 commit 250348a

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
- Add `InvalidCharacterException` to throw an exception if the input string contains invalid characters.
10+
- Add `RemoveSeparators` method to remove all separators from a string. Use it, for example, with credit card numbers.
1011

1112
### Changed
1213
- Renamed `Luhn.ConvertAlphaNumericToNumeric` to `Luhn.AlphaNumericToNumeric`

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ namespace Example7
274274
{
275275
public static void Main(string[] args)
276276
{
277-
string creditCardNumberWithoutCheckDigit = "4417 1234 5678 911".Replace(" ", "");
277+
string creditCardNumberWithoutCheckDigit = "4417 1234 5678 911".RemoveSeparators();
278278
byte checkDigit = creditCardNumberWithoutCheckDigit.ComputeLuhnCheckDigit();
279279
Console.WriteLine($"The check digit for credit card number {creditCardNumberWithoutCheckDigit} is: {checkDigit}");
280280
}
@@ -295,7 +295,7 @@ namespace Example8
295295
{
296296
public static void Main(string[] args)
297297
{
298-
string creditCardNumber = "4417 1234 5678 9113".Replace(" ", "");
298+
string creditCardNumber = "4417 1234 5678 9113".RemoveSeparators();
299299
bool isValid = creditCardNumber.IsValidLuhnNumber();
300300
Console.WriteLine($"The credit card number {creditCardNumber} is valid: {isValid}");
301301
}

src/Luhn.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ namespace LuhnDotNet
3737
using System.Collections.Generic;
3838
using System.Diagnostics.CodeAnalysis;
3939
using System.Globalization;
40-
#if !NET8_0_OR_GREATER
4140
using System.Text;
41+
#if !NET8_0_OR_GREATER
4242
using System.Text.RegularExpressions;
4343
#endif
4444

@@ -234,6 +234,34 @@ public static bool IsValidLuhnCheckDigit(this byte checkDigit, string number)
234234
#endif
235235
}
236236

237+
/// <summary>
238+
/// Removes all separators from the input identifier string.
239+
/// </summary>
240+
/// <param name="identifier">The input identifier string with separators.</param>
241+
/// <returns>The identifier string without separators.</returns>
242+
/// <exception cref="ArgumentNullException">Thrown when the input identifier is null or empty.</exception>
243+
public static string RemoveSeparators(this string identifier)
244+
{
245+
if (string.IsNullOrWhiteSpace(identifier))
246+
{
247+
throw new ArgumentNullException(nameof(identifier), "The identifier cannot be null or empty.");
248+
}
249+
250+
// Define valid separators as a HashSet for O(1) lookup
251+
var separators = new HashSet<char> { '/', '\\', '-', '_', '|', ',', ' ', '.' };
252+
var result = new StringBuilder();
253+
254+
foreach (char c in identifier)
255+
{
256+
if (!separators.Contains(c))
257+
{
258+
result.Append(c);
259+
}
260+
}
261+
262+
return result.ToString();
263+
}
264+
237265
/// <summary>
238266
/// Converts an alphanumeric string to a numeric string.
239267
/// </summary>

tests/LuhnTest.cs

+16
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,21 @@ public void ComputeLuhnCheckDigit_WithAlphaNumericToNumeric_ReturnsExpectedCheck
379379
Assert.Equal(expected, input.AlphaNumericToNumeric().ComputeLuhnCheckDigit());
380380
Assert.Equal(expected, input.AlphaNumericToNumeric().AsSpan().ComputeLuhnCheckDigit());
381381
}
382+
383+
/// <summary>
384+
/// Tests that the RemoveSeparators method processes input correctly by removing separators.
385+
/// </summary>
386+
/// <param name="input">The input string containing separators.</param>
387+
/// <param name="expected">The expected string after separators are removed.</param>
388+
[Theory]
389+
[InlineData("4444 5555 6666 8888", "4444555566668888")]
390+
[InlineData("6666/5555/7777/9123", "6666555577779123")]
391+
[InlineData(@"6666\5555\7777\9123", "6666555577779123")]
392+
[InlineData("4321-5678-9012-3456", "4321567890123456")]
393+
[InlineData("0987654321", "0987654321")]
394+
public void RemoveSeparators_ValidInput_ReturnsExpectedResult(string input, string expected)
395+
{
396+
Assert.Equal(expected, input.RemoveSeparators());
397+
}
382398
}
383399
}

0 commit comments

Comments
 (0)