mikezang
mikezang

Reputation: 2489

How to split string by multiple delimiters in flutter?

This looks like a simple question, but I couldn't find any result after google. I have string tel:090-1234-9876 03-9876-4321 +81-90-1987-3254, I want to split it to tel:, 090-1234-9876, 03-9876-4321 and +81-90-1987-3254, what can I do?

Upvotes: 2

Views: 8881

Answers (2)

prahack
prahack

Reputation: 1599

Simply you can use the split() method of the Dart as follows.

final str = "tel:090-1234-9876 03-9876-4321 +81-90-1987-3254";
print(str.split(" "));

If you want to use any other pattern, try splitting using regex.

final str = "tel: 090-1234-9876 03-9876-4321 +81-90-1987-3254";
print(str.split(RegExp(r'\s')));
// outputs: [tel:, 090-1234-9876, 03-9876-4321, +81-90-1987-3254]

Additionally, if you want to split by multiple delimiters e.g by +, -, and \s then

final str = "tel: 090-1234-9876 03-9876-4321 +81-90-1987-3254";
print(str.split(RegExp(r'[+-\s]')));
// outputs: [tel:, 090, 1234, 9876, 03, 9876, 4321, , 81, 90, 1987, 3254]

Try Dart Pad

Upvotes: 10

sidrao2006
sidrao2006

Reputation: 1328

Use dart's String.split(Pattern) method!!

String telnums = 'tel:090-1234-9876 03-9876-4321 +81-90-1987-3254';

List<String> splitValues = telnums.split(" ");

In fact, you can use any regex (Regex), String or pattern (Pattern)

Upvotes: 0

Related Questions