在Dart中将英语数字与波斯语数字转换

时间:2018-11-20 13:31:33

标签: dart flutter

我是Dartflutter的新朋友。

我想用波斯数字代替英文数字。 如何实现呢?

1-2-3-4-5-6-7-8-9 ==> ‍‍۱-۲-۳-۴-۵-۶-۷-۸-۹

3 个答案:

答案 0 :(得分:4)

示例:

String replaceFarsiNumber(String input) {
  const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

  for (int i = 0; i < english.length; i++) {
    input = input.replaceAll(english[i], farsi[i]);
  }

  return input;
}

main() {
  print(replaceFarsiNumber('0-1-2-3-4-5-6-7-8-9'));  // ==>  ۰-۱-۲-۳-۴-۵-۶-۷-۸-۹
}

答案 1 :(得分:1)

不确定在什么情况下要使用数字,但是我宁愿定义一个const映射: const numberMap = {1: '۰', 2: '۱', 3:'۲', 4:'۳', 5:'۴', 6:'۵', 7:'۶', 8:'۷', 9:'۸',0: '۹'}; 然后,您只需调用numberMap[number]即可重复使用它。

答案 2 :(得分:0)

class A {    
    int i;
    // only authorized entities can create B
    class B_PrivateCreationToken {};    
    friend class B;

    template<typename AType>
    static auto getB(AType&& a) {
        std::shared_ptr<B> shared_b = a.my_B.lock();
        if (!shared_b){
            shared_b = std::make_shared<B> (
                  std::forward<AType>(a),
                  B_PrivateCreationToken{} );
            a.my_B = shared_b;
        } 
        return shared_b;
    }
public:

    // public part as in above version...

private:
    mutable std::weak_ptr<B> my_B;
};