Gabe Newell
Gabe Newell

Reputation: 35

How can I select a part from a big string starting in point A and ending in point B

My string is:

[...]
<div class="threads">
<a href="/adgagshsdh_t_54654321654" title="Title 2">
<a href="/adgagsdfdfhsdh_t_44654321654" title="Title 3">
<a href="/adgagsdfhsdh_t_54321654" title="Title 4">
<a href="/adgagsdfhsdfhsdh_t_14654321654" title="Title 5">
[...etc]
</div>
[...]

I want to take everything out except for _t_54654321654 and then after that everything but 54654321654.

Final string should look like this

54654321654

I tried with indexOf but didn't really work.

I tried searching but I didn't know how exactly to describe what I was trying to do.

Upvotes: 0

Views: 160

Answers (4)

Damith
Damith

Reputation: 63065

string whatYouNeed = "/adgadgdfhdsdfgsadfgdsg_t_54654321654".Split('_').Last();

Or

string whatYouNeed = "<a href=\"/adgadgdfhdsdfgsadfgdsg_t_54654321654\" title=\"Title\">"
    .Split('_')
    .Last()
    .Split(new string[] {"\""},StringSplitOptions.RemoveEmptyEntries)
    .First();

But If you want to work with HTML content you better use Html Agility Pack

Check this question which some what similar to this and by using html agility pack

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.Load("test.html");
var link = htmlDoc.DocumentNode
                  .Descendants("a")
                  .First(); // assume it is First link tag what you need

string hrefValue = link.Attributes["href"].Value;
string whatYouNeed = hrefValue.Split('_').Last();

Answers to your comments

but what if I have the source code inside an string and not in a test.html document?

You can load the html as below

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);

Also there's dozens of "<a href"'s with different numbers in them in the source code, I don't need a specific one, just one of them.

var links = htmlDoc.DocumentNode
                      .Descendants("a").ToList();

above will return all the links in the page you can get any item you want like links[1] or links[3] etc ...

Upvotes: 4

KbManu
KbManu

Reputation: 418

Try this:

        int index = s.LastIndexOf('_') + 1;
        string t = s.Substring(index, s.Length - index);

Upvotes: 0

sudipto
sudipto

Reputation: 2482

Try:

  Regex rgx = new Regex("^.+?_t_([^\"]+?)\".+?$");
  string result = rgx.Replace(yourString, "$1");

Explanation: It matches characters from the begining of the string using ^ and moves till _t_ and then sub-matches in $1 anything till " and then " and then anything till end $.

The whole string is replaced by the sub-match $1.

Upvotes: 0

Atish Kumar Dipongkor
Atish Kumar Dipongkor

Reputation: 10422

Try it by the sub string method

Like

string sub = mainString.Substring(startPosition, endPosition);

Upvotes: 0

Related Questions