8

I want to split some string and to contain each line in separate element in array. I use this code for reaching my goal:

List _masForUsing;
_masForUsing = new List(numberOfLines);

    void gpsHelper(String text, int count){
        for(int i =0; i<count;i++){
          _masForUsing[i] = text.split("\\r?\\n");
        }
        print(_masForUsing[2]);
      }

But when I am truying to evoke the third element of array, it just show full my string. Please help, me :)

This is example of String:

real time: 23-09-2019, 23:08:55, Mon 
system time: 5362 
gps coordinates: 55.376538; 037.431973; 
gps satellites: 3; 
gps time: 20:09:02; 
gps date: 23.09.19;
temperature: 19 *C 
humidity: 35 %  
dust: 1 
zivert: 21
1
  • 1
    Your split argument is a string, but you are expecting it to be a regular expression. It doesn't match because your string does not contain the five-letter substring "BACKSLASH r QUESTIONMARK BACKSLASH n". To use it as a RegExp, you have to write text.split(RegExp("\\r?\\n")) (or, more readable: text.split(RegExp(r"\r?\n")) using a raw string). Or use the available StringSplitter as recommended below. Commented Oct 1, 2019 at 7:30

2 Answers 2

22

Use Dart's built in LineSplitter, It is system independent.

void gpsHelper(String text) {
   LineSplitter ls = new LineSplitter();
   List<String> _masForUsing = ls.convert(text);

    //print(_masForUsing[2]); // gps coordinates: 55.376538; 037.431973;
}
Sign up to request clarification or add additional context in comments.

1 Comment

If anyone wonders, you need to import 'dart:convert';
-3

Is this what you want?

void gpsHelper(String text) {
  List _masForUsing = text.split("\n");//this one here

  print('third string is ${_masForUsing[2]}');
}

1 Comment

I added example, which i want to split

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.