skip to Main Content

I am trying to divide a string in flutter into text and numbers.
The string consists of a number and a measurement in the metric system. My goal is to separate the number from the measure in order to process them separately

This is information in g (gram) kg (kilogram) l (litre) and ml (millilitre) and potentially more.
the output of the method should have values that are given in grams and millilitres only. The others should be calculated down to these. How can I easily implement this in dart, as an endless if query seems wrong to me.

Example values:
"22g"=> ["22","g"] |"150 g"=> ["150","g"] |"120ml"=> ["120","ml"] |"2 l" => ["2000","ml"] |"1kg" => ["1000","g"]

My current way is like this:

List<String> splitString(String input) {
    input = input.replaceAll(" ", "");
    if (input.contains("ml")) {
      String number = input.replaceAll("ml", "");
      return [number, "ml"];
    } else if(input.contains("g")){
      String number = input.replaceAll("g", "");
      return [number, "g"];
    } else if(input.contains("kg")){
      String number = input.replaceAll("kg", "");
      double value = double.parse(number) * 1000;
      return [value.toStringAsFixed(2), "g"];
    } else{
      String number = input.replaceAll("l", "");
      double value = double.parse(number) * 1000;
      return [value.toStringAsFixed(2), "ml"];
    }
  }

2

Answers


  1. Try this!

    This code takes a string input, such as ’22g’, and splits it into its numeric and alphabetic parts. It utilizes a regular expression to identify the pattern of one or more digits followed by one or more alphabetic characters. If the input matches this pattern, it returns a list containing the numeric and alphabetic parts; otherwise, it returns an empty list. Finally, the result is printed.

    void main() {
      String input = "22g";
      List<String> result = splitString(input);
      print(result); // Output: [22, g]
    }
    
    List<String> splitString(String input) {
      RegExp regex = RegExp(r'^(d+)([a-zA-Z]+)$');
      Match? match = regex.firstMatch(input) as Match?;
      if (match != null) {
        String numberPart = match.group(1)!;
        String alphabetPart = match.group(2)!;
        return [numberPart, alphabetPart];
      } else {
        return [];
      }
    }
    

    If you have any problem, feel free to ask me.

    Login or Signup to reply.
  2. it’s old school, try, but it’s preferable for strings that contains empty space between numbers and chars eg: 120 l not 120l

     List<String> splitter(String str) {
          str.trim();
          var list = str.split(' ');
    // for now, work is done. the coming is the unit Measurement conversion
        
          if (list[1].trim() == 'l') {
            list[0] = (int.parse(list[0].trim()) * 1000).toString();
            list[1] = 'ml';
          } else if (list[1].trim() == 'kg') {
            list[0] = (int.parse(list[0].trim()) * 1000).toString();
            list[1] = 'g';
          }
          return list;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search