Reputation: 359
I am receiving data in a string like this
IN-PATIENT AND DAYCARE TREATMENT (HOSPITALIZATION)FOR ONLY PARENTS ||||||||||ACCIDENTAL INJURIES=COVERED|||||MEDICAL EMERGENCIES=COVERED FROM OPD AND OPD REIMBURSEMENT BASIS
I want to show this in listview. And I need to break it by ||||| and split it by =
Mean in the list it will show like
IN-PATIENT AND DAYCARE TREATMENT (HOSPITALIZATION)FOR ONLY PARENTS
ACCIDENTAL INJURIES : COVERED
MEDICAL EMERGENCIES : COVERED FROM OPD AND OPD REIMBURSEMENT BASIS
The issue is how can I break it by |||? because sometimes the length of | is 5 sometimes 10 and so on. I just want whenever | is come it's just skip remaining | and just break it.
I am doing like this
List wordings = wording.split('||||');
print(wordings);
List splited;
wordings.forEach((element) {
splited = element.split('=');
var displayPolicy = {'name': splited[0], 'value': splited[1]};
displayData.add(displayPolicy);
});
It's working but the issue in this is its only break when |||| is four i want to skip this at any length
Edit
I am now doing like this
String wording = data['records'][0]['policies'][0]['policywording'];
List wordings = wording.split(RegExp(r"[|]+"));
print(wordings);
List splited;
wordings.forEach((element) {
splited = element.split('=');
var displayPolicy = {'name': splited[0], 'value': splited[1]};
displayData.add(displayPolicy);
});
print(displayData);
when I print wordings it's showing like this
[IN-PATIENT AND DAYCARE TREATMENT (HOSPITALIZATION), , ||ACCIDENTAL INJURIES=COVERED, |MEDICAL EMERGENCIES=COVERED FROM OPD]
It's not splitting and showing an error
Upvotes: 0
Views: 668
Reputation: 358
Because the first element of your list wording ("IN-PATIENT AND DAYCARE TREATMENT (HOSPITALIZATION)FOR ONLY PARENTS") doesn't contain '=' , so now first element of your List splited is "IN-PATIENT AND DAYCARE TREATMENT (HOSPITALIZATION)FOR ONLY PARENTS" and there's no second element, that's why it's throwing a range error,, because there's no index 1, simply the length of splited is 1, and you're trying to access second element, that's not possible.
Make sure the length of the list splited is 2, and if it's not add an empty string for index 1;
check this example
List wordings = wording.split('||||');
print(wordings);
List splited;
wordings.forEach((element) {
splited = element.split('=');
if(splited.lenght <2){
splited.add('n/a');
}
var displayPolicy = {'name': splited[0], 'value': splited[1]};
displayData.add(displayPolicy);
});
Upvotes: -1
Reputation: 2089
use regexp to split
List wordings = wording.split(RegExp(r'\|+'));
List wordings = wording.split(RegExp(r'\|+'));
print(wordings);
List splited;
wordings.forEach((element) {
splited = element.split('=');
var displayPolicy = {'name': splited[0], 'value': splited.length > 1 ? splited[1] : ''};
displayData.add(displayPolicy);
});
print(displayData);
Upvotes: 1
Reputation: 1227
Above both answers are correct you need to add some condition because on your string all values don't have "=" sign so that's why its showing this error. You can handle it like this
List wordings = wording.split(RegExp(r'\|+'));
List splited;
wordings.forEach((element) {
if (element.contains("=")) {
splited = element.split('=');
var displayPolicy = {'name': splited[0], 'value': splited[1]};
displayData.add(displayPolicy);
} else {
var displayPolicy = {'name': element, 'value': ''};
displayData.add(displayPolicy);
}
});
if your data don't have "=" it will just add in "name" of displayPolicy
and if its have "=" sign then they have both name and value. you can manage it by your pattern now.
Upvotes: 0
Reputation: 177
You can try like this if you want
String text = "IN-PATIENT AND DAYCARE TREATMENT (HOSPITALIZATION)FOR ONLY PARENTS ||||||||||ACCIDENTAL INJURIES=COVERED|||||MEDICAL EMERGENCIES=COVERED FROM OPD AND OPD REIMBURSEMENT BASIS";
List sentences = text.split('|');
sentences.forEach((wordPart){
if(wordPart != ""){
// Here do your another split for the equals
}
});
Upvotes: 0
Reputation: 3218
you can use regex
wording.split(RegExp(r"[|]+"));
this site is a good place to learn https://regexone.com/
Upvotes: 2
Reputation: 2678
I would split it by '|' and store it in a list, parse the list and remove empty elements.
Upvotes: -1