• support@answerspoint.com

how to Get string between two strings

1843

My string is: "show-234-public", i want to get the number after "show-" and before "-public", it is "234". I have tried with following code but it returns an empty result:

$string = 'show-234-public';
$display = preg_replace('/show-(.*?)-public/','',$string);
echo $display;

 

2Answer


0

Perhaps, a good way is just to cut out a substring:

String St = "super exemple of string key : text I want to keep - end of my string";

int pFrom = St.IndexOf("key : ") + "key : ".Length;
int pTo = St.LastIndexOf(" - ");

String result = St.Substring(pFrom, pTo - pFrom);
  • answered 8 years ago
  • B Butts

0
string input = "super exemple of string key : text I want to keep - end of my string";
var match = Regex.Match(input, @"key : (.+?)-").Groups[1].Value;

or with just string operations

var start = input.IndexOf("key : ") + 6;
var match2 = input.Substring(start, input.IndexOf("-") - start);
  • answered 8 years ago
  • G John

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 1843 times
  • active 8 years ago

Best Rated Questions