編輯:單串缺少了(
... 8(對不起,浪費你的時間String.Split和Regex.Split不工作?
,我有以下格式的字符串列表:
"Some Text (1234-567890)"
我試圖使用string.Split
或Regex.Split
拆分此所述(
周圍,然後拉出從第一元件前面的文字和數字的文本斷第二元件
例:
string myText = "Some Text (1234-567890)";
string[] myValues = myText.Split('(');
string first = myValues[0]; // should hold "Some Text "
string second = myValues[1]; // should hold "1234-567890)"
取而代之的是,我得到這個是否使用string.Split
或Regex.Split
,是一個包含單個值而沒有(
的數組。
例:
string myText = "Some Text (1234-567890)";
string[] myValues = myText.Split('(');
string first = myValues[0]; // has "Some Text 1234-567890)"
string second = myValues[1]; // exception; there is no second value
如果我使用Regex.Split
也會發生這種情況。例如:
string[] myValues = Regex.Split(myText, "\\(");
如果我嘗試把它放到一個新的項目中,它可以像我期望的那樣工作。兩者之間的唯一區別是我使用Excel interop填充List<string>
。我不明白爲什麼會有所作爲。
我的實際代碼如下所示:
const int LOCATION_START = 2;
const int LOCATION_END = 39;
List<string> locationStrings = new List<string>();
for (int row = LOCATION_START + 1; row <= LOCATION_END; row++)
locationStrings.Add(pExcel.ActiveSheet.ReadValue<string>(row));
List<Tuple<string, string>> locations = new List<Tuple<string, string>>();
foreach (string locationString in locationStrings)
{
string[] values = Regex.Split(locationString, "\\(");
locations.Add(Tuple.Create(values[0].Trim(), values[1].Substring(0, 11)));
//^this line throws an exception, because there is only one value, not two
}
Excel.ActiveSheet.ReadValue
使用互操作範圍功能,從一個Excel工作表中讀取值並將它轉換爲string
。
您可以發佈解決此'從Excel互操作的列表'一些更多的細節? –
2012-08-09 23:36:01