将字母数字字符串转换为salesforce apex中的数字字符串

时间:2015-01-19 15:05:17

标签: salesforce numeric apex alphanumeric

我需要将电话号码转换为数字字符串。 例如+ 1222333-456789至1222333456789。

使用salesforce apex的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

对于那些需要类似事物的人来说,这就是我做到的:

if (num.isNumeric()) {
    return num;
}
else {
    String n = '';
    for (Integer i=0; i<num.length(); i++) {
        if (num.substring(i, i+1).isNumeric()) {
            n += num.substring(i, i+1);
        }
    }
    return n;
}

随时建议我如何改进代码。

答案 1 :(得分:0)

这可以使用正则表达式完成:

String phoneNumber = '(987) 654-3210';

// Remove non-digit characters
String phoneNumberDigits = phoneNumber.replaceAll('\\D+','');
long phoneNumberInt = Long.parseLong(phoneNumberDigits);
相关问题