[英]Get specific string after specific character
我有几个字符串:
<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}
)?
您可以使用IndexOf
方法获取第一次出现的\\
(inputChar),然后使用SubString
方法获取剩余的字符串。
var result = str.Substring(str.IndexOf(inputChar) + 1);
如果给定字符不存在于给定字符串中,则需要处理这种情况。
根据您的要求,更好的方法是创建正则表达式以匹配您想要的字符串,然后删除不相关的字符。 我已经实现了以下代码,请检查。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.