2015-02-06 36 views
0

我有一個字符串如何得到一個字符串的特定部分在C#

string a = "(something is there),xyz,(something there)"; 

和,我用這個

string s = "(something is there),xyz,(something there)"; 
int start = s.IndexOf("(") + 1; 
int end = s.IndexOf(")", start); 
string result = s.Substring(start, end - start); 

,但我想用第二部分(something there) 我該怎麼辦它?

+1

正則表達式會有所幫助。或者你可以使用'LastIndexOf',或者你可以使用'IndexOf'重載,它允許你指定搜索起始索引(使用最後一次搜索的索引+1)。或者你可以分開','並拿走最後一個元素。 – 2015-02-06 15:46:06

+1

正則表達式有什麼問題? '@「\([^()] * \)(?= [^()] * $)「' – 2015-02-06 15:46:16

+0

你是否總是想要第二個'('作爲開始還是你總是希望兩者的內容? – Hogan 2015-02-06 15:48:26

回答

0

如果你想獲得第二個「(」和「)」之間,然後如果你想獲得後的字符串中使用這臺開始索引搜索

start = s.IndexOf("(", end) + 1; 
end = s.IndexOf(")", start); 
string secondResult = s.Substring(start, end - start); 

IndexOf第二個參數的文本最後)使用此代碼:

string otherPart = s.Substring(end+1); 
3

您可以使用此:

string s = "(something is there),xyz,(something there)"; 
    var start = s.Split(',')[2]; 

你也可以使用:

 string s = "(something is there),xyz,(something there)"; 
     Regex regex = new Regex(@"\([^()]*\)(?=[^()]*$)"); 
     Match match = regex.Match("(something is there),xyz,(something there)"); 
     var result = match.Value; 
+1

完全搞不清楚爲什麼有人會對此置之不理,看起來像是* specific *問題的最簡單的解決方案作爲OP介紹 – 2015-02-06 15:51:13

+0

@MattBurland沒問題,我不回答投票 – 2015-02-06 15:52:37

+1

爲什麼字符串a =必填? – 2015-02-06 15:52:43

5

不知道到底是什麼你解決這個做的,但是這確實是在這種特殊情況下:

var last = s.Split(',').Last(); // "(something there)" 

或者更冗長的解釋:

var s = "(something is there),xyz,(something there)"; 

var split = s.Split(','); // [ "(something is there)", "xyz", "(something there)" ] 

var last = split.Last(); // "(something there)" 

如果你不想要括號(英國)

var content = last.Trim('(', ')'); // "something there" 
2

您可以使用下面的,如果你只是想要的文字:

var s = "(something is there),xyz,(something there)"; 
var splits = s.Split('('); 
var text = splits[2].Trim(')'); 
3
a.Split("(),".ToCharArray(),StringSplitOptions.RemoveEmptyEntries); 

這將返回數組與3串:something is therexyzsomething there

+0

但是OP不會**需要'xyz'。 – MickyD 2015-02-06 16:16:46

+0

@MickyDuncan - 可能不會 - 要求非常模糊。我認爲這是執行'spit()'的最好方法,而其他人沒有顯示它。 – Hogan 2015-02-06 16:18:55

3

如果 「最後」在這種情況下,您可以使用「第二個」String.LastIndexOf

string lastPart = null; 
int lastStartIndex = a.LastIndexOf('('); 
if (lastStartIndex >= 0) 
{ 
    int lastEndIndex = a.LastIndexOf(')'); 
    if (lastEndIndex >= 0) 
     lastPart = a.Substring(++lastStartIndex, lastEndIndex - lastStartIndex); 
} 

這裏是一個提取從字符串中的所有標記爲List<string>的解決方案:如果列表太小

string first = tokens[0]; 
string second = tokens.ElementAtOrDefault(1); 

int startIndex = -1, endIndex = -1; 
var tokens = new List<string>(); 
while (true) 
{ 
    startIndex = a.IndexOf('(', ++endIndex); 
    if (startIndex == -1) break; 
    endIndex = a.IndexOf(')', ++startIndex); 
    if (endIndex == -1) break; 
    tokens.Add(a.Substring(startIndex, endIndex - startIndex)); 
} 

所以,現在你可以使用索引或Enumerable.ElementAtOrDefault你會得到null。如果你只是想最後使用tokens.Last()

+0

可能比其他答案有點冗長? – MickyD 2015-02-06 16:20:10

+0

@MickyDuncan:我提供了一個更詳細的方法,可以提取所有部分。 – 2015-02-06 16:40:28

相關問題