我有几个字符串:
<img title=\"\\angle X < \\angle Y < \\angle Z\" src=\"http://latex.codecogs.com/gif.latex?\\angle&space;X&space;<&space;\\angle&space;Y&space;<&space;\\angle&space;Z\" />
我想得到angle&space;X&space;<&space;\\angle&space;Y&space;<&space;\\angle&space;Z
和
<img title=\"1,25 - \\frac{5}{6} \\times 280\\% \\div 1\\tfrac{1}{6}\" src=\"http://latex.codecogs.com/gif.latex?1,25&space;-&space;\\frac{5}{6}&space;\\times&space;280\\%&space;\\div&space;1\\tfrac{1}{6}\" />
我想得到:
frac{5}{6}&space;\\times&space;280\\%&space;\\div&space;1\\tfrac{1}{6}
代码:
string imgSoal;
string imgName = imgSoal.Split("\\")[1];
我遇到了问题,即我从上面的代码中获得的字符串是angle&space;X&space;<&space;
和frac{5}{6}&space;
如何获得我想要的字符串(angle&space;X&space;<&space;\\angle&space;Y&space;<&space;\\angle&space;Z
和frac{5}{6}&space;\\times&space;280\\%&space;\\div&space;1\\tfrac{1}{6}
)?
答案 0 :(得分:1)
您可以使用IndexOf
方法来获取\\
(inputChar)的第一个匹配项,然后使用SubString
方法来获取其余的字符串。
var result = str.Substring(str.IndexOf(inputChar) + 1);
如果给定字符串中不存在给定字符,则需要处理。
答案 1 :(得分:0)
根据您的要求,更好的方法是创建正则表达式以匹配所需的字符串,然后删除不相关的字符。我已经执行了以下代码,请检查。
private string stringA = "<img title=\"\\angle X < \\angle Y < \\angle Z\" src=\"http://latex.codecogs.com/gif.latex?\\angle&space;X&space;<&space;\\angle&space;Y&space;<&space;\\angle&space;Z\" />";
private string stringB = "<img title=\"1,25 - \\frac{5}{6} \\times 280\\% \\div 1\\tfrac{1}{6}\" src=\"http://latex.codecogs.com/gif.latex?1,25&space;-&space;\\frac{5}{6}&space;\\times&space;280\\%&space;\\div&space;1\\tfrac{1}{6}\" />";
private string patternA = @"\?(.*?)/>";
private string patternB = @";\\frac(.*?)/>";
foreach (Match match in Regex.Matches(stringA, patternA))
{
Console.WriteLine(match.Value);
var tem = match.Value.Remove(0, 2);
var res = tem.Substring(0, tem.Length - 4);
}
foreach (Match match in Regex.Matches(stringB, patternB))
{
Console.WriteLine(match.Value);
var tem = match.Value.Remove(0, 2);
var res = tem.Substring(0, tem.Length - 4);
}