c#中的char数组到c#

时间:2011-12-18 20:12:56

标签: c#

我有一个看起来像电话号码的字符串。我正在使用lambda来提取char数组。然后我想将此数组转换为字符串。这就是我所拥有的:

PhoneCandidate = "(123)-321-1234"; //this is just an example of what it could look like
var p = PhoneCandidate.Where(c => char.IsDigit(c)).ToArray();

string PhoneNumber = p.toString();

但是,当我运行此代码时,变量PhoneNumber变为“System.Char []”而不是数组中的值。

我需要改变什么?

感谢。

7 个答案:

答案 0 :(得分:4)

您可以使用string constructor that takes a char[]

string PhoneNumber = new string(p);

答案 1 :(得分:3)

string phone = new string(p);

答案 2 :(得分:2)

尝试使用constructor res = new string(yourArray);

答案 3 :(得分:2)

其中一个string constructors需要char[]

string PhoneNumber = new string(p);

答案 4 :(得分:1)

让我们采取一个全新的方向:

Dim digits as New Regex(@"\d");
string  phoneNumber = digits.Replace(PhoneCandidate, "");

答案 5 :(得分:0)

假设pchar[],您可以使用以下内容:

String phoneNumber = new String(p);

答案 6 :(得分:0)

最好的办法是使用每个字符串构造函数(@Adam Robinson),但作为替代方法,您也可以使用string.Join(string separator,params string[] value)MSDN docs here

PhoneCandidate = "(123)-321-1234"; //this is just an example of what it could look like
var p = PhoneCandidate.Where(c => char.IsDigit(c)).ToArray();

string str = string.Join(string.Empty,p);