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?
Asked
Active
Viewed 6,985 times
2

sidrao2006
- 1,228
- 2
- 10
- 32

mikezang
- 2,291
- 7
- 32
- 56
-
use `[]` for regex as `or` and put any sign you want to split : `str.split(RegExp(r'[ :]'));` – yellowgray Jan 03 '21 at 13:34
2 Answers
9
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]

Mahesh Jamdade
- 17,235
- 8
- 110
- 131

prahack
- 1,267
- 1
- 15
- 19
0
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
)

sidrao2006
- 1,228
- 2
- 10
- 32
-
what can I split `:`, ` ` and ` `? you only split one of them what I knew... – mikezang Jan 03 '21 at 12:48
-
@mikezang, you could use regex to do that and could you please explain what do you mean by 'only split one of them'? – sidrao2006 Jan 03 '21 at 13:28
-
Your example didn't split on colon. Colon is still part of the first element returned. – Randal Schwartz Jan 03 '21 at 18:06