2009-10-22 40 views
0

我有一個源代碼的HTML字符串。如何刪除字符串中的字符從點X到開始

所以我會去:

int X = find indexof("theterm"); 
thesourcecode = thesourcecode.Substring(???? 

如何刪除從點theterm背後發現的所有字符?感謝所以。

編輯:一個例子,讓人們不會感到困惑。

樣本字符串:「大紅房子真是太大了!」

int Position = sampleString.IndexOf(「house」);

)從點位置,並回復,刪除了一切:

結果字符串我的方法後:「是如此enourmous

+0

這個問題是有點混亂,措詞將高度建議。 – mgbowen 2009-10-22 21:23:00

回答

2
// this could be set explicitly or variable based on user input. 
    string mySearchString = "TextToFind"; 

下面的代碼假定這意志!改變,否則我會用數字10代替mySearchString.Length

int foundIndex = myString.IndexOf(mySearchString); 

一旦你找到的指數很容易:

您的字符串

myString = myString.SubString(0, foundIndex); 

之前拆除所有的文本或搜索文本後刪除所有文本。

myString = myString.SubString(foundIndex + mySearchString.Length, myString.Length - 1); 
+0

xD我簡直不敢相信它那麼簡單。多謝兄弟。 – 2009-10-22 21:23:23

+1

在第二個示例中,您不需要子字符串中的第二個參數。使用一個參數時的默認值是直到字符串結尾。 – BFree 2009-10-22 21:25:12

+1

你的例子似乎有點混亂。不應該將「刪除」替換爲「獲取」?或者「之前」和「之後」被交換並且調整? – 2009-10-22 21:30:37

1

如果你的意思是刪除一個角色之前的所有文字,你會怎麼做:

string s = "i am theterm"; 
int index = s.IndexOf("theterm"); 
s = s.Substring(index, s.Length - index); 
0

只需將寫

thesourcecode = thesourcecode.Substring(X); 

舉例來說,如果我們做了以下內容:

string s = "Hello there everybody!"; 
s = s.Substring(s.IndexOf("the")); 

s now now e 「每個人都有!」

0
thesourcecode = thesourcecode.Remove(0, thesourcecode.IndexOf("theterm")); 
+0

您必須添加搜索文本的長度。 – 2009-10-22 21:33:55

0

未經測試:

var index = thesourcecode.IndexOf("theterm"); 
thesourcecode = thesourcecode.Substring(index); 
相關問題