Ad

Flutter, Dart Split Sentence Every One Character To 2 Characters Size, For Flutter Custom Tts Project

- 1 answer

Example:

var sentence = (hello there, I am Bob.)

var result = [ 'he', 'el', 'll', 'lo', ' ', 'th', 'he', 'er', 're', ',', ' ', 'I', ' ', 'am', ' ', 'bo', 'ob', '.']

I've found here working example, though it is in Javascript and I don't really know how to adopt it for Dart, and not sure how will behave once white space and punctuation is added in. Punctation and white space I need always split on its own not in combination with letters, I need them as well, as I will use them to add pauses in between words and sentences.

Thank you

    var a = 12345678;
    a= a.toString();
    var arr=[];
    for (var i =0; i<a.length-1; i++) {
     arr.push(Number(a[i]+''+a[i+1]));
    }
    
    console.log(arr);

Ad

Answer

If someone is having same issue, this is how I solved it

void main(String string) {
var test = "I Hello there I am Bob 23!";

List<String> nameArray = test.split('');

for (int curIndex = 0; curIndex < nameArray.length; curIndex++) {

if (curIndex >= 1 && nameArray[curIndex].contains(new RegExp(r'[a-zA-Z]')) && nameArray[curIndex-1].contains(new RegExp(r'[a-zA-Z]'))) {
      print(nameArray[curIndex-1] + nameArray[curIndex]); // checks if current curIndex and previous curIndex are letters, if so returns previous and curent letters joined
    } else {
if  (curIndex >= 1 && nameArray[curIndex].contains(new RegExp(r'[a-zA-Z]')) && nameArray[curIndex+1].contains(new RegExp(r'[a-zA-Z]'))) {
      null; // checks if curIndex and next curIndex are letters, if so returns null
  }else{
    print(nameArray[curIndex]);
  }

}
}
}

Which returns

I

He
el
ll
lo
 
th
he
er
re
 
I
 
am
 
Bo
ob
 
2
3
!
Ad
source: stackoverflow.com
Ad