2015-07-10 54 views
1

我想搜索一個字符串中的一個字符串BeginsWith和EndsWith東西,然後最終用一個標記替換它。查找字符串與BeginsWith和EndsWith

所以,讓我們說我有一個字符串:

"This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!" 

我想提取/識別以下字符串:

<TokenID=123>doesn't matter</Token> 

所以,我可以一個替代語句中的使用原始字符串,用其他東西代替它。這個標籤的ID可能是不同的,所以我想通過使用類似識別上面的字符串:

var beginsWith = "<TokenID="; 
var endsWith = "</Token>"; 

的BeginsWith和的endsWith值將從CSV拉文件到列表中,因爲有許多的他們有不同的內容。一旦我知道如何提取這些字符,我最終希望將它們替換爲可以分割的字符,以便在提取的字符串周圍創建一個字符串數組。

我不想使用正則表達式,因爲BeginsWith和EndsWith字符串需要易於配置並添加到稍後階段。

這感覺就像它應該是一個非常簡單的練習,但對我的生活我無法弄清楚如何做到這一點?

+0

可能需要使用正則表達式。像'。*' – DLeh

+0

你在哪裏看到'你好'?另外,如果它的開頭是「 Hello there'。請告訴我們具體的規則。你不想提取'123'? –

+0

您是否在尋找[String.IndexOf()](https://msdn.microsoft.com/en-us/library/7cct0x33%28v=vs.110%29.aspx)? –

回答

1

如果我正確理解你的問題,你想要使用IndexOf() & Substring()

IndexOf()將讓你可以爲它提供給Substring()

string data = "This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!"; 
string beginsWith = "<TokenID="; 
string endsWith = "</Token>"; 

int startIndex = data.IndexOf(beginsWith); 
// Add the length of endWidth so you're getting the location of the last character of the endsWith 
int endIndex = data.IndexOf(endsWith) + endsWith.Length; 
string extract = data.Substring(startIndex, endIndex - startIndex); 
Console.WriteLine(extract); 
Console.ReadLine(); 

結果你beginsWith和ENDWITH的位置:

<TokenID=123>doesn't matter</Token> 

如果你改變主意有關使用Regex,就可以仍然使用你的startsWith和endsWith來創建你的模式。

string data = "This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!"; 
string beginsWith = "<TokenID="; 
string endsWith = "</Token>"; 

string extract = Regex.Match(data, String.Format("{0}.+{1}", beginsWith, endsWith)).Value; 
Console.WriteLine(extract); 
Console.ReadLine(); 

String.Format()創建一個看起來像

<TokenID=.+</Token> 

結果的模式:

<TokenID=123>doesn't matter</Token> 
+0

Troll down vote?請對給定答案的投票結果發表評論。 – Shar1er80

+0

我選擇了這個,因爲它們大部分都是相同的答案,但這是第一個,它起作用。謝謝 – Mark

0

這裏是一個擴展方法,以便您可以只需將它連接到一個字符串:

方法

private static string StringBetween(this string StringEval, string startsWith, string endsWith) 
    { 
     var str = StringEval; 
     var start = str.IndexOf(startsWith); 
     var end = str.IndexOf(endsWith, start); 


     var val = str.Substring(start, (end - start) + endsWith.Length); 
     return val; 
    } 

使用

static void Main(string[] args) 
     { 
      var exampleStr = "This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!"; 
      var startsWith = "<TokenID="; 
      var endsWith = "</Token>"; 

      var val = exampleStr.StringBetween(startsWith, endsWith); 

      Console.WriteLine(val); 

      Console.ReadKey(); 
     } 
0

不使用正則表達式:

string s="This is text with a token: <TokenID=123>doesn't matter</Token>, and and some more text!" 
string beginsWith = "<TokenID="; 
string endsWith = "</Token>"; 

string Extract = null ; 
int i,j ; 
if ((i=s.IndexOf(beginsWith))>=0) && ((j=s.Substring(i).IndexOf(endsWith)>=0)) 
    Extract=s.Substring(i,j)+endsWith ; 
// result in extract (null if delimiting strings not found)