从字符串中删除格式:“((123)456-7890&";=>" 1234567890&quot ;? [英] Remove formatting from a string: "(123) 456-7890" => "1234567890"?

查看:33
本文介绍了从字符串中删除格式:“((123)456-7890&";=>" 1234567890&quot ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入电话号码时,我有一个字符串-有一个掩码,所以它总是看起来像(123)456-7890"-我想先将格式保存下来,然后再保存到数据库中.

I have a string when a telephone number is inputted - there is a mask so it always looks like "(123) 456-7890" - I'd like to take the formatting out before saving it to the DB.

我该怎么做?

推荐答案

使用linq的一种可能性是:

One possibility using linq is:

string justDigits = new string(s.Where(c => char.IsDigit(c)).ToArray());

通过craigmoliver添加更简洁的版本

Adding the cleaner/shorter version thanks to craigmoliver

string justDigits = new string(s.Where(char.IsDigit).ToArray())

这篇关于从字符串中删除格式:“((123)456-7890&";=>" 1234567890&quot ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆