Home > Software design >  Get string after array of characters
Get string after array of characters

Time:04-27

I got a string like this:

"{cat: Molly, dog: Feefee}"

I want save in a String cat name, and in different String dog name. How can I do it?

I tried manipulating with constant indexes, but I wnt to be sure, the name will be saved properly when there are different names

CodePudding user response:

Maybe you're looking for it?

import 'dart:convert';

void main() {
  final a = '{"cat": "Molly", "dog": "Feefee"}';
  final b = json.decode(a);
  final map = Map<String, dynamic>.from(b);

  print(map['cat']);
  print(map['dog']);
}

Output:

Molly
Feefee

CodePudding user response:

I would suggest doing some data cleaning so that your data comes out in JSON format. Then you can decode that and convert it to a map. However, if that is not possible. Here is a round a bout way of getting the values you want.

String example = "{cat: Molly, dog: Feefee}";
  
var re = RegExp(r'(?<={)(.*)(?=})');
var match = re.firstMatch(example)?.group(0);
  
var splitted = match?.split(",");
  
var cat = splitted?[0].split(":")[1];
print(cat); //prints Molly
  • Related