[英]array of char to string in 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 []”,而不是數組中的值。
我需要更改什么?
謝謝。
您可以使用采用char[]
的string
構造函數 。
string PhoneNumber = new string(p);
string phone = new string(p);
\n
嘗試使用構造函數 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);
最好的選擇也許是使用每個(@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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.