简体   繁体   English

如何使用indexof和substring解析特定的字符串?

[英]How can i parse specific string using indexof and substring?

int firstTag = source.IndexOf("data-token=");
int lastTag = source.IndexOf("\"href", firstTag);
int startIndex = firstTag + 12;
int endIndex = lastTag + 5;
string authenticityToken = source.Substring(startIndex, endIndex - startIndex);

The string I want to parse is from here: 我要解析的字符串是从这里:

<a class="bizLink" data-token="-iUzEhgdscgbpj5VMi5zoh54FTeFt8M4mj5nsiodxR5VzZOhniodpj6nFQg0nce3MhUxFSgdxjM4J
jUVzZuNu8o0sREnFSUzISUXzZWh4iodGQfdxR5VzZWh4iodGQfhli6fnce_=" 
                           href="

I want to get only the string between " and " only this: 我只想获取“和”之间的字符串:

-iUzEhgdscgbpj5VMi5zoh54FTeFt8M4mj5nsiodxR5VzZOhniodpj6nFQg0nce3MhUxFSgdxjM4J
    jUVzZuNu8o0sREnFSUzISUXzZWh4iodGQfdxR5VzZWh4iodGQfhli6fnce_=

But what I get with my code is this long string I wanted, but also all the rest of the file text. 但是我得到的代码是我想要的这个长字符串,还有所有其余的文件文本。

The sane way would be to use a HTML parser and querying library. 理智的方法是使用HTML解析器和查询库。 I can suggest CsQuery , which is a jQuery-like library in .NET. 我可以建议使用CsQuery ,它是.NET中类似jQuery的库。 You could use a selector like a[data-token] to match your anchor, then extract the attribute value. 您可以使用类似a[data-token]的选择器来匹配锚,然后提取属性值。

This is the correct way of doing things. 这是正确的做事方式。


But if you only ever want to get this one attribute and don't do anything with the HTML source ever again, it might be easier to just use a regex, but beware: parsing HTML with regex is evil . 但是,如果您只想得到这一个属性,不要再碍着与HTML源东西,它可能会更容易,只需使用正则表达式,但要注意: 解析HTML与正则表达式是邪恶的

So if all you want to do is just extract this one piece of information, as an exceptional measure, for your information, you could use that: 因此,如果您要做的只是提取一条信息(作为一种特殊的措施)作为您的信息,则可以使用以下信息:

var m = Regex.Match(source, @"data-token\s*=\s*""(?<token>.+?)""");
var authenticityToken = m.Groups["token"].Value;

But try CsQuery first. 但是请先尝试使用CsQuery。 It's a much better approach. 这是一个更好的方法。

Working example http://ideone.com/U224iZ 工作示例http://ideone.com/U224iZ

string start = "data-token=";
  string end = " href";

  string source = "<a class='bizLink' data-token='-iUzEhgdscgbpj5VMi5zoh54FTeFt8M4mj5nsiodxR5VzZOhniodpj6nFQg0nce3MhUxFSgdxjM4JjUVzZuNu8o0sREnFSUzISUXzZWh4iodGQfdxR5VzZWh4iodGQfhli6fnce_=1\" href='";

  int firstTag = source.IndexOf(start);
  int lastTag = source.IndexOf(end, firstTag );
  int startIndex = firstTag + start.Length +1;
  int endIndex = lastTag;
  string authenticityToken = source.Substring(startIndex, endIndex - startIndex -1);
  Console.Write(authenticityToken);
  Console.ReadLine();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用IndexOf解析特定的字符串? - How can i parse specific string using IndexOf? 我如何解析字符串中的文本并将其添加到列表中 <string> 使用indexof和substring? - How can i parse text from string and add it to a List<string> using indexof and substring? 如何使用IndexOf和Substring从字符串解析文件名? - How can I parse a file name from string using IndexOf and Substring? 如何使用indexof和substring和string.format在文本的特定位置添加随机数/文本? - How can I use indexof and substring and string.format to add random numbers/text in specific place in text? 如何使用 indexof 和 substring 在字符串中查找单词? - How can I use indexof and substring to find words in a string? 如何使用 indexof 和 substring 从字符串中提取数字并制作列表<int>的数字?</int> - How can I use indexof and substring to extract numbers from a string and make a List<int> of the numbers? 使用indexof和substring时,如何解析正确的开始索引和结束索引? 我该如何编码希伯来字符? - When using indexof and substring how do i parse the right start and end indexs ? And how do i encode hebrew chars? 如何使用indexof和substring或HtmlAgilityPack从文本部分获取数字? - How can i get the numbers from a text part using indexof and substring or maybe HtmlAgilityPack? 如何使用 indexof 和 substring 从字符串中提取特定文本,然后如何将提取的字符串格式化为日期时间? - How to use indexof and substring to extract specific text from a string and then how to format the extracted string to date time? 恼人的字符串子字符串和IndexOf - Annoying String Substring & IndexOf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM