1

I have following strings :

String? hello = "(1.2,1.5 | 5)"
String? hi = "(2.3,3.2 | 9)"

Now I want to get

var newhello1 = 1.2,1.5
var newhello2 = 5

and

var newhi1 = 2.3,3.2
var newhi2 = 9

How to extract those text from that entire strings?

2
  • newhello1 will be a list? Commented Jul 29, 2022 at 8:59
  • No. It will be a string only. Commented Jul 29, 2022 at 9:08

2 Answers 2

1

You can use the indexOf function combined with the substring to get the substrings as follows

var newhello1 = hello.substring(hello.indexOf('(') + 1, hello.indexOf('|')).trim(); //Use Trim() to get rid of any extra spaces
var newhello2 = hello.substring(hello.indexOf('|') + 1,hello.indexOf(')')).trim();
print(newhello1); //1.2,1.5
print(newhello2); //5
Sign up to request clarification or add additional context in comments.

2 Comments

It will be a string only
okay, I'll edit the answer to keep the relevant part only.
0
List<String> myformatter(String? data) {
  if (data == null) return [];
  List<String> ls = data.split("|");

  for (int i = 0; i < ls.length; i++) {
    ls[i] = ls[i].replaceAll("(", "").replaceAll(")", "").trim();
  }
  return ls;
}

main() {
  String? hello = "(1.2,1.5 | 5)";
  String? hi = "(2.3,3.2 | 9)";

  final helloX = myformatter(hello);

  print(helloX[0]); //1.2,1.5 
  print(helloX[1]); //5

  final hiX = myformatter(hi);
  print(hiX[0]); //2.3,3.2 
  print(hiX[1]); //9
}

Comments

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.