Pedro Massango
Pedro Massango

Reputation: 5015

Dart: replace the first n digits in a String

I have a string of numbers and I need to replace the first N digits of it using regex.

I tried the following code but it is not working:

 String hideLastFourCharacters(String s){
    final result = s.replaceAll(r"\\d{2}", '-');
    return result;
  }

Upvotes: 2

Views: 4738

Answers (5)

Ephenodrom
Ephenodrom

Reputation: 1893

Don't reinvent code if there is already a package for this :

Github: https://github.com/Ephenodrom/Dart-Basic-Utils

Pub Dev : https://pub.dev/packages/basic_utils

Install :

dependencies:
  basic_utils: ^1.5.1

Usage :

// Your case
String s = StringUtils.hidePartial(stringToHide, begin: 0, end: stringToHide.lengh - 4);

// Other examples
String s = StringUtils.hidePartial("1234567890");
print(s); // "*****67890"
String s = StringUtils.hidePartial("1234567890", begin: 2, end: 6);
print(s); // "12****7890"
String s = StringUtils.hidePartial("1234567890", begin: 1);
print(s); // "1****67890"
String s = StringUtils.hidePartial("1234567890", begin: 2, end: 14);
print(s); // "12********"

Other usefull methods from the StringUtils Class :

String defaultString(String str, {String defaultStr = ''});
bool isNullOrEmpty(String s);
bool isNotNullOrEmpty(String s);
String camelCaseToUpperUnderscore(String s);
String camelCaseToLowerUnderscore(String s);
bool isLowerCase(String s);
bool isUpperCase(String s);
bool isAscii(String s);
String capitalize(String s);
String reverse(String s);
int countChars(String s, String char, {bool caseSensitive = true});
bool isDigit(String s);
bool equalsIgnoreCase(String a, String b);
bool inList(String s, List<String> list, {bool ignoreCase = false});
bool isPalindrome(String s);
String hidePartial(String s, {int begin = 0, int end, String replace = "*"});
String addCharAtPosition(String s, String char, int position,{bool repeat = false});

Upvotes: 0

Spatz
Spatz

Reputation: 20118

To replace only a given number of digits, you can use replaceFirst method:

 var re = RegExp(r'\d{2}'); // replace two digits
 print('123456789'.replaceFirst(re, '--')); // --3456789

If you need to replace all but the last n given digits, you can use replaceAll with negative lookahead:

 var re = RegExp(r'\d(?!\d{0,2}$)'); // keep last 3 digits
 print('123456789'.replaceAll(re, '-')); // ------789

Negative lookahead (?! exclude matches followed by n-1 or less digits \d{0,2} at the end $).

Upvotes: 5

krolikmvp
krolikmvp

Reputation: 11

You can do:

s = N*'-' + s[N:]

for the first N digits and:

s = s[:-N] + N*'-'

for the last N digits.

Edit:

Nevermind, it was tagged as python at first

Upvotes: 0

Pedro Massango
Pedro Massango

Reputation: 5015

I found a temporary solution for this by using:

 String hideLastFourCharacters(String s){
    final lastTwoDigits = s.substring(s.length-2, s.length);
    return lastTwoDigits.padLeft(9, '*');
  }

Upvotes: 0

AbsoluteSpace
AbsoluteSpace

Reputation: 760

One option is to remove the first N characters of the string, and then add N copies of '-' to the beginning.

This could be done like:

var result = s.substring(0, N);

return s.padLeft(s.length + N, '-');

Upvotes: 0

Related Questions