<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