[英]Match exact string using string.Contains() method in c#
I have an html content. 我有一个html内容。 I'm parsing the content using HtmlAgilityPack.
我正在使用HtmlAgilityPack解析内容。 I Need to replace attribute
我需要替换属性
' align = "middle" ' with ' align = "center" ', I'm using function ' align =“ middle” '与' align =“ center” ',我正在使用函数
if(htmlDoc.DocumentNode.OuterHttml.Contains("align = "middle""))
htmlDoc.DocumentNode.OuterHttml.Replace("align = "middle","align = "center"")
But if condition is returning true even for **valign = "middle"**
! 但是,即使条件对于
**valign = "middle"**
返回true!
What is that i need to put in if condition other than Contains()
? 如果条件不是
Contains()
,我需要输入什么?
yes I'm trying to find match inside an html content.
是的,我正在尝试在html内容中找到匹配项。
Then use HtmlAgilityPack . 然后使用HtmlAgilityPack 。 Your code would be something like.
您的代码将是类似的。
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(HtmlString);
var tds = doc.DocumentNode.SelectNodes("//td[@align='middle']");
or something like this using LINQ 或类似的使用LINQ
var tds = doc.DocumentNode.Descendants("td")
.Where(td => td.Attributes["align"].Value == "middle")
.ToList();
It not that clear what you are trying to achieve. 尚不清楚您要实现的目标。
String.Contains
checks if a given string is contained in another (probably larger) string, so if it is a substring. String.Contains
检查给定字符串是否包含在另一个(可能更大)的字符串中,是否为子字符串。
Maybe you want to know if they are same, then use Equals
or ==
: 也许您想知道它们是否相同,然后使用
Equals
或==
:
bool same = string1 == string2;
or you want to know if it starts with a given string, then use StartsWith
: 或者您想知道它是否以给定的字符串开头,然后使用
StartsWith
:
bool startsWith = string1.StartsWith(string2);
or you want to ignore the case (.NET is case sensitive): 或者您想忽略大小写(.NET区分大小写):
bool equalsIgnoreCase = string1.Equals(string2, StringComparison.CurrentCultureIgnoreCase);
the same with StartsWith
: 与
StartsWith
相同:
bool startsWithIgnoreCase = string1.StartsWith(string2, StringComparison.CurrentCultureIgnoreCase);
finally the case-insensitive contains equivalent using IndexOf
: 最后,不区分大小写的内容包含使用
IndexOf
等效项:
bool containsIgnoreCase = string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0;
if your string is like "blablabla align = 'align = \\"middle\\" blablabla" then you can do: 如果您的字符串类似于“ blablabla align ='align = \\“ middle \\” blablabla”,则可以执行以下操作:
Contains(" align = \"middle\" ") // spaces before and after
But how others says it's hard to understand what you want exactly. 但是其他人怎么说很难理解您到底想要什么。
You could try something like: 您可以尝试类似:
string str = "valign = \"middle\"";
string search = "align = \"middle\"";
int ix;
while ((ix = str.IndexOf(search, ix)) != -1)
{
if (ix == 0 || !char.IsLetterOrDigit(str[ix - 1]))
{
break;
}
ix++;
}
bool success = ix != -1;
This will work even for valign = "middle";align = "middle"
这甚至适用于
valign = "middle";align = "middle"
It checks if the letter that precede the beginning of the match is a non- IsLetterOrDigit
(if present). 它检查比赛开始之前的字母是否为非
IsLetterOrDigit
(如果存在)。 If yes, it break
s, otherwise it return searching for a match from the next character. 如果是,则
break
s,否则返回从下一个字符开始搜索匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.