c#中字符到字符串的数组
本文关键字:数组 字符串 字符 | 更新日期: 2023-09-27 18:18:18
我有一个看起来像电话号码的字符串。我使用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[]"而不是数组中的值。
我需要改变什么?
谢谢。
您可以使用接受char[]
的string
构造函数
string PhoneNumber = new string(p);
string phone = new string(p);
尝试使用构造函数res = new string(yourArray);
一个string
构造函数取char[]
:
string PhoneNumber = new string(p);
让我们从一个全新的方向出发:
Dim digits as New Regex(@"'d");
string phoneNumber = digits.Replace(PhoneCandidate, "");
假设p
是char[]
,您可以使用以下命令:
String phoneNumber = new String(p);
可能最好的选择是使用字符串构造函数per (@Adam Robinson),但是作为另一种选择,您也可以使用string.Join(string separator,params string[] value)
(MSDN文档在这里)
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);