Tester12
Tester12

Reputation: 1011

How to extract currency value from string using regex in dart?

I'm trying to extract the currency symbol €$ from string using regex. For Example, if my string contains $40.00, I need to get only the $ symbol from the string, Please guide. Example String values: $40.00, 45$,€65,89£.Need to extract any currency.

Upvotes: 0

Views: 593

Answers (1)

julemand101
julemand101

Reputation: 31219

A tactic is to just remove anything from the String which are not part of the number.

void main() {
  print(getCurrencySymbol(r'$40.00'));     // $
  print(getCurrencySymbol(r'45$'));        // $
  print(getCurrencySymbol(r'€65,89'));     // €
  print(getCurrencySymbol(r'100,89 DKK')); // DKK
}

String getCurrencySymbol(String value) =>
    value.replaceAll(RegExp(r'[-.,0-9 ]'), '');

Upvotes: 2

Related Questions