2012-08-09 34 views
1

編輯:單串缺少了( ... 8(對不起,浪費你的時間String.Split和Regex.Split不工作?

,我有以下格式的字符串列表:

"Some Text (1234-567890)" 

我試圖使用string.SplitRegex.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.SplitRegex.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

+0

您可以發佈解決此'從Excel互操作的列表'一些更多的細節? – 2012-08-09 23:36:01

回答

1

您顯示的代碼按預期工作。獲得該結果的唯一方法是該字符串不包含任何開始括號,例如包含"Some Text 1234-567890)"而不是"Some Text (1234-567890)"

這也有可能是你有一些不同尋常的字符,看起來像在一個環境出發括號,但在另一個無法打印的字符。

您應該檢查,當你從Exfel片讓他們你的琴絃實際上包含。

+0

是的,你是對的。我錯誤地斷定這兩個元素是以某種方式融合回單一元素的。起初我以爲這是LINQ爲了一些奇怪的原因而將枚舉扁平化。但是,不,你是對的,列表中的其中一個值缺少'(完全。 – 2012-08-10 00:11:47

0
string Source = "Some Text (1234-567890)"; 
string[] Splitted = Regex.Split(Source, @"\("); 
foreach (string Item in Splitted) 
Console.WriteLine(Item.Replace(")",""); //Use replace if you want to remove the closing bracket. 
//Map the values 
string First = Splitted[0]; 
string Second = Splitted[1].Replace(")",""); 

您需要轉義左括號。它對於正則表達式引擎有特殊的意義。

+0

你的答案是做什麼的真的很長的路要走看看我的例子運行和測試都屬於你和我我發佈的內容效率更高。 – MethodMan 2012-08-10 00:02:04

0

此代碼應工作:

string[] myValues = myText.Split(new string[] { "(" }, StringSplitOptions.None); 
+0

IWIH檢查代碼如果你運行該代碼,它仍然在myValues [1] – MethodMan 2012-08-09 23:55:28

+0

@DJ KRAZE中保留「)」對於超級遲到的響應抱歉,但是,我沒有通過那段時間的筆記本電腦......要從代碼應該是這樣的: string [] myValues = myText.Split(new string [] {「(」,「)」},StringSplitOptions.None); – IWIH 2012-09-15 12:53:01

0

數據的格式不正確。一行是這樣形式的:

"Some Text 1234-567890)" 

所以Split方法只返回那個字符串。

0

如果你想這樣做,這是一個使用你的代碼的解決方案我剛剛添加了一個新行 myValues [1] = myValues [1]。更換(」)」,」」); 這消除惱人的「)」字符,你看到 你原來的代碼工作刪除「(」但之間的差異「(」和「)」

這裏是一個更好的例子路過分離PARAMS一個「」

string myText = "Some Text (1234-567890)"; 
string[] myValues = myText.Split('(',')'); 
string first = myValues[0]; // should hold "Some Text " 
string second = myValues[1];