[英]When using indexof and substring how do i parse the right start and end indexs ? And how do i encode hebrew chars?
我有以下代码:
string firstTag = "Forums2008/forumPage.aspx?forumId=";
string endTag = "</a>";
index = forums.IndexOf(firstTag, index1);
if (index == -1)
continue;
var secondIndex = forums.IndexOf(endTag, index);
result = forums.Substring(index + firstTag.Length + 12, secondIndex - (index + firstTag.Length - 50));
我要从中提取的字符串例如:
<a href="/Forums2008/forumPage.aspx?forumId=317" title="הנקה">הנקה</a>
我想要得到的是标题后的单词,仅此: הנקה
第二个问题是,当我提取它时,我看到希伯来语有些杂乱,像这样:
一种有效的方法是使用正则表达式,而不是尝试查找起始位置并使用子字符串。 试用此代码,您将看到它提取了anchor标签的标题:
var input = "<a href=\"/Forums2008/forumPage.aspx?forumId=317\" title=\"הנקה\">הנקה</a>";
var expression = new System.Text.RegularExpressions.Regex(@"title=\""([^\""]+)\""");
var match = expression.Match(input);
if (match.Success) {
Console.WriteLine(match.Groups[1]);
}
else {
Console.WriteLine("not found");
}
出于好奇,这是JavaScript的一个版本:
var input = '<a href="/Forums2008/forumPage.aspx?forumId=317" title="הנקה">הנקה</a>'; var expression = new RegExp('title=\\"([^\\"]+)\\"'); var results = expression.exec(input); if (results) { document.write(results[1]); } else { document.write("not found"); }
好的,这里是使用String.Substring()
String.Split()
和String.IndexOf()
的解决方案
String str = "<a href=\"/Forums2008/forumPage.aspx?forumId=317\" title=\"הנקה\">הנקה</a>"; // <== Assume this is passing string. Yes unusual scape sequence are added
int splitStart = str.IndexOf("title="); // < Where to start splitting
int splitEnd = str.LastIndexOf("</a>"); // < = Where to end
/* What we try to extract is this : title="הנקה">הנקה
* (Given without escape sequence)
*/
String extracted = str.Substring(splitStart, splitEnd - splitStart); // <=Extracting required portion
String[] splitted = extracted.Split('"'); // < = Now split with "
Console.WriteLine(splitted[1]); // <= Try to Out but yes will produce ???? But put a breakpoint here and check the values in split array
现在的问题是,在这里您可以看到我必须以一种不寻常的方式使用转义序列。 您可能会忽略这一点,因为您只是在传递扫描字符串。
这确实有效,但是您无法使用提供的Console.WriteLine(splitted[1]);
可视化它Console.WriteLine(splitted[1]);
但是,如果放置一个断点并检查提取的拆分数组,则可以看到已提取文本。 您可以使用以下屏幕截图进行确认
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.