0

I'm trying to code a scraper in C#, the thing is I have already made one in PHP but I'm trying to migrate on C#.

Can this possibly be converted on C# as I'm not yet good in coding with this language.

PHP Function:

function fetchValue($string,$start,$end){
    $str = explode($start,$string);
    $str = explode($end,$str[1]);
    return $str[0];
}

What it exactly does is it extracts the first occurence string between the START and the END.

How is it used:

$exampleString = "long live stackoverflow";
$fetchString = fetchValue($exampleString,"long "," stackoverflow";
echo $fetchString;

This will then output the word "live" as the START was "long" and the END was "stackoverflow"

Every answers will much be appreciated! Thanks in advance!

CedeeCQ
  • 45
  • 1
  • 1
  • 5

1 Answers1

0

Use the Split method from the C#'s String class; see this reference

Don't forget to add using System.Text.RegularExpressions; along with the other using statements

public string ExtractValue(string source, string start, string end) {
    string[] str = Regex.Split(source, start);
    string[] str2 = Regex.Split(str[1], end);
    return str2[0];
}
Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
  • Thank you for your approach and the effort yet unfortunately I'm unable to use it due to error: Cannot convert from string to char – CedeeCQ Jan 18 '19 at 17:16
  • Any possible fix about it? – CedeeCQ Jan 18 '19 at 17:17
  • Thank you for your quick response. I really appreciate it. Accepted this as an answer. But btw, do you think this is the best approach in doing such task? – CedeeCQ Jan 18 '19 at 17:24
  • I imagine you're going to use this to extract values from child nodes in the `DOM` and you're going to call it like `string child = ExtractValue(parent, "
    ",
    ");`. Regular expressions are quite effective when it comes to parsing text they are used by compilers all around the world.
    – Some random IT boy Jan 18 '19 at 17:33