<span style="cursor: pointer;">$$</span>
如何從此字符串中獲得<span style="cursor: pointer;">
和</span>
。 $$附近的一切。如何拆分字符串?
不需要是正則表達式。
<span style="cursor: pointer;">$$</span>
如何從此字符串中獲得<span style="cursor: pointer;">
和</span>
。 $$附近的一切。如何拆分字符串?
不需要是正則表達式。
嗯,你可以只使用:
string[] bits = input.Split(new string[]{"$$"}, StringSplitOptions.None);
注意,與服用char
分隔符重載,你有提供StringSplitOptions
,你有手動創建陣列。 (當然,如果你打算重複這樣做,你可能會想重新使用這個數組。)你也可以選擇指定分割輸入的最大數量的標記。請參閱string.Split overload list瞭解更多信息。
短,但完整的程序:
using System;
public static class Test
{
public static void Main()
{
string input = "<span style=\"cursor: pointer;\">$$</span>";
string[] bits = input.Split(new string[]{"$$"},
StringSplitOptions.None);
foreach (string bit in bits)
{
Console.WriteLine(bit);
}
}
}
當然,如果你知道有隻打算爲兩個部分,另一種選擇是使用IndexOf
:
int separatorIndex = input.IndexOf("$$");
if (separatorIndex == -1)
{
throw new ArgumentException("input", "Oh noes, couldn't find $$");
}
string firstBit = input.Substring(0, separatorIndex);
string secondBit = input.Substring(separatorIndex + 2);
或者你可以使用的IndexOf來找到「$$」,然後使用Substring方法來獲得任何一方。
例
string s = "<span style=\"cursor: pointer;\">$$</span>";
string findValue = "$$";
int index = s.IndexOf(findValue);
string left = s.Substring(0, index);
string right = s.Substring(index + findValue.Length);
我只是添加正是我的答案:) – 2011-03-23 17:15:09
:)我期望從Skeet – 2011-03-23 17:21:54
thx的傳說中得到同樣的幫助。 – senzacionale 2011-03-23 17:44:01
您可以使用一個子()方法:
string _str = "<span style=\"cursor: pointer;\">$$</span>";
int index = _str.IndexOf("$$");
string _a = _str.Substring(0, index);
string _b = _str.Substring(index + 2, (_str.Length - index - 2));
米蒂亞
UU THX那真是快 – senzacionale 2011-03-23 17:13:28