'How to get the first two words in a string Flutter
I have this string example: Hello my name is
And this is what I want to show: Hello my
What is the best practice to do it?
Thanks in advance
Solution 1:[1]
Split, then take(2) and then join them again:
final src = 'Hello my name is';
final result = src.split(' ').take(2).join(' ');
print(result);
Solution 2:[2]
The easiest way is to break down the sentence into a List of words :
String test = 'Hello my name is';
List<String> words = test.split(" ");
print('${words[0]} ${words[1]}');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | WSBT |
| Solution 2 | esentis |
