2011-03-05 91 views
15

我有一個很大的字符串,它存儲在一個字符串變量str中。我想從c#中獲取一個子字符串? 假設字符串爲:" Retrieves a substring from this instance. The substring starts at a specified character position."如何從c#中的字符串獲取子串?

子串是什麼結果我想顯示爲:The substring starts at a specified character position.

+3

你爲什麼要使用子串?有可能有更好的方法來做你需要的東西,然後用一個神奇的數字來使用Substring,但由於你沒有告訴我們你的主要目標是什麼,所以很難提出更好的方法。 – 2011-03-05 10:02:13

回答

16
string newString = str.Substring(0,10) 

會給你前10個字符(從位置0到位置9)。

請參閱here

4

這裏是從14個字符到字符串結束獲取子字符串的示例。您可以修改它以符合您的需求

string text = "Retrieves a substring from this instance. The substring starts at a specified character position."; 
//get substring where 14 is start index 
string substring = text.Substring(14); 
+0

adrian,對不起,我通常不會做downvotes(很容易),但我不得不這樣做。不僅這有點'不安全',你也沒有跟隨任何邏輯,爲什麼你選擇了魔法#14(實際上,我們把'我們從'這個字符串......')。我只是不明白。再次,對此感到遺憾,但我覺得我必須指出這一點。 – 2011-03-05 11:37:19

+0

14只是一個隨機數,我不覺得應該爲Riya計算字符串的字符,他可以自己做這件事......我的回答只是如何獲得部分字符串的一個示例,我不介意不打算給這個完整的解決方案。在我看來,對於這樣簡單的問題,它更好地指向正確的方向,而不是給出完整的代碼複製和粘貼... – 2011-03-05 12:20:01

+1

adrian - 我是基於我的毛病在事實問題明確性問:子串結果我想顯示的是:'子字符串從指定的字符位置開始。因此,我無法在答案中看到相關(除了將子字符串作爲字面解釋的問題)。那就是說,我明白你的意思,而不是完整的答案。現在所有最好的 – 2011-03-05 12:46:50

19

您可以手動執行此操作或使用IndexOf方法。

手動:

int index = 43; 
string piece = myString.Substring(index); 

使用的IndexOf,你能看到的句號是:

int index = myString.IndexOf(".") + 1; 
string piece = myString.Substring(index); 
+0

+1還建議一種不使用幻數的方法。雖然真的這個問題不清楚需要做什麼以及爲什麼。 – 2011-03-05 10:03:46

+1

我會建議'int index = 1 + myString.IndexOf(「。」); string piece = myString.Substring(index);'省略'。' – 2011-03-05 10:08:28

+0

編輯,謝謝:) – 2011-03-05 10:10:51

2

日亞,

使要拆分的句號(假設。 ),那麼這裏有一個方法可以捕獲所有發生的事件:

// add @ to the string to allow split over multiple lines 
// (display purposes to save scroll bar appearing on SO question :)) 
string strBig = @"Retrieves a substring from this instance. 
      The substring starts at a specified character position. great"; 

// split the string on the fullstop, if it has a length>0 
// then, trim that string to remove any undesired spaces 
IEnumerable<string> subwords = strBig.Split('.') 
    .Where(x => x.Length > 0).Select(x => x.Trim()); 

// iterate around the new 'collection' to sanity check it 
foreach (var subword in subwords) 
{ 
    Console.WriteLine(subword); 
} 

享受...

1
string text = "Retrieves a substring from this instance. The substring starts at a specified character position. Some other text"; 

string result = text.Substring(text.IndexOf('.') + 1,text.LastIndexOf('.')-text.IndexOf('.')) 

這將減少其特殊字符之間的字符串奠定的一部分。

0
var data =" Retrieves a substring from this instance. The substring starts at a specified character position."; 

var result = data.Split(new[] {'.'}, 1)[0]; 

輸出:

檢索從此實例的子串。子字符串從 指定的字符位置開始。

0

it's easy to rewrite this code in C#...

此方法,如果你的價值是2子之間!

例如:

stringContent = "[myName]Alex[myName][color]red[color][etc]etc[etc]" 

調用應該是:

myNameValue = SplitStringByASubstring(stringContent , "[myName]") 

colorValue = SplitStringByASubstring(stringContent , "[color]") 

etcValue = SplitStringByASubstring(stringContent , "[etc]") 
+1

首先,你應該將你的代碼作爲文本發佈,圖像可以被斷開鏈接並且不能被搜索。其次,如果你認爲你的答案是正確的,你應該使用OP的樣本 – Prisoner 2016-11-02 07:43:02

相關問題